Files
catalyst/zipline/utils/munge.py
T
Dale Jung 38e8d5214d PERF: History Perf Enhancements
Limited use of `pandas` data structures in both `HistoryContainer` and
`RollingPanel`. Where possible, methods were amended to return raw
`ndarrays` with the indexing logic done separately. This allows us to
cut down the number of times pandas objects are created both as returns
and intermediate values. The separation of indexing from data access
allowed us to minimize the times we’d make use of pandas indexes.

This required that that certain methods like `NDFrame.ffill` be replaced
with versions that work with `ndarrays`. Some of this was done via
straight numpy methods and others by access pandas internal
machinery. Outside of allowing us to use faster ndarrays, many of these
function provided speedups over their pandas counterparts as we didn’t
require the extra features like handling multiple dtypes. i.e. np.isnan
is faster than pd.isnull, but only works with certain dtypes.
2015-02-11 06:25:53 -05:00

74 lines
2.2 KiB
Python

#
# Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas.core.common as com
def _interpolate(values, method, axis=None):
if values.ndim == 1:
axis = 0
elif values.ndim == 2:
axis = 1
else:
raise Exception("Cannot interpolate array with more than 2 dims")
values = values.copy()
values = interpolate_2d(values, method, axis=axis)
return values
def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None):
"""
Copied from the 0.15.2. This did not exist in 0.12.0.
Differences:
- Don't depend on pad_2d and backfill_2d to return values
- Removed dtype kwarg. 0.12.0 did not have this option.
"""
transf = (lambda x: x) if axis == 0 else (lambda x: x.T)
# reshape a 1 dim if needed
ndim = values.ndim
if values.ndim == 1:
if axis != 0: # pragma: no cover
raise AssertionError("cannot interpolate on a ndim == 1 with "
"axis != 0")
values = values.reshape(tuple((1,) + values.shape))
if fill_value is None:
mask = None
else: # todo create faster fill func without masking
mask = com.mask_missing(transf(values), fill_value)
# Note: pad_2d and backfill_2d work inplace in 0.12.0 and 0.15.2
# in 0.15.2 they also return a reference to values
if method == 'pad':
com.pad_2d(transf(values), limit=limit, mask=mask)
else:
com.backfill_2d(transf(values), limit=limit, mask=mask)
# reshape back
if ndim == 1:
values = values[0]
return values
def ffill(values, axis=None):
return _interpolate(values, 'pad', axis=axis)
def bfill(values, axis=None):
return _interpolate(values, 'bfill', axis=axis)