Skip to content

Commit

Permalink
MAINT: Remove outdated compat code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Sanderson committed Aug 1, 2016
1 parent a5f3662 commit 03efb1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 124 deletions.
48 changes: 23 additions & 25 deletions tests/pipeline/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@
)
from zipline.utils.memoize import lazyval
from zipline.utils.numpy_utils import bool_dtype
from zipline.utils.pandas_utils import (
ewma,
ewmstd,
rolling_apply,
rolling_mean,
)


class RollingSumDifference(CustomFactor):
Expand Down Expand Up @@ -961,16 +955,16 @@ def test_SMA(self):
# Shift back the raw inputs by a trading day because we expect our
# computed results to be computed using values anchored on the
# **previous** day's data.
expected_raw = rolling_mean(
DataFrame(
expected_bar_values_2d(
dates - self.trading_calendar.day,
self.equity_info,
'close',
),
expected_raw = DataFrame(
expected_bar_values_2d(
dates - self.trading_calendar.day,
self.equity_info,
'close',
),
).rolling(
window_length,
min_periods=1,
).mean(
).values

expected = DataFrame(
Expand Down Expand Up @@ -1082,10 +1076,11 @@ def expected_ewma(self, window_length, decay_rate):
# Don't use it outside of testing. We're using rolling-apply of an
# ewma (which is itself a rolling-window function) because we only want
# to look at ``window_length`` rows at a time.
return rolling_apply(
self.raw_data,
window_length,
lambda window: ewma(DataFrame(window), span=span).values[-1],
return self.raw_data.rolling(window_length).apply(
lambda subarray: (DataFrame(subarray)
.ewm(span=span)
.mean()
.values[-1])
)[window_length:]

def expected_ewmstd(self, window_length, decay_rate):
Expand All @@ -1096,10 +1091,11 @@ def expected_ewmstd(self, window_length, decay_rate):
# EWMSTD. Don't use it outside of testing. We're using rolling-apply
# of an ewma (which is itself a rolling-window function) because we
# only want to look at ``window_length`` rows at a time.
return rolling_apply(
self.raw_data,
window_length,
lambda window: ewmstd(DataFrame(window), span=span).values[-1],
return self.raw_data.rolling(window_length).apply(
lambda subarray: (DataFrame(subarray)
.ewm(span=span)
.std()
.values[-1])
)[window_length:]

@parameterized.expand([
Expand Down Expand Up @@ -1224,7 +1220,7 @@ def test_dollar_volume(self):
expected_1 = (self.raw_data[5:] ** 2) * 2
assert_frame_equal(results['dv1'].unstack(), expected_1)

expected_5 = rolling_mean((self.raw_data ** 2) * 2, window=5)[5:]
expected_5 = ((self.raw_data ** 2) * 2).rolling(5).mean()[5:]
assert_frame_equal(results['dv5'].unstack(), expected_5)

# The following two use USEquityPricing.open and .volume as inputs.
Expand All @@ -1234,9 +1230,11 @@ def test_dollar_volume(self):
* self.raw_data[5:] * 2).fillna(0)
assert_frame_equal(results['dv1_nan'].unstack(), expected_1_nan)

expected_5_nan = rolling_mean((self.raw_data_with_nans
* self.raw_data * 2).fillna(0),
window=5)[5:]
expected_5_nan = ((self.raw_data_with_nans * self.raw_data * 2)
.fillna(0)
.rolling(5).mean()
[5:])

assert_frame_equal(results['dv5_nan'].unstack(), expected_5_nan)


Expand Down
3 changes: 1 addition & 2 deletions zipline/pipeline/loaders/blaze/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
optionally,
)
from zipline.utils.numpy_utils import bool_dtype, categorical_dtype
from zipline.utils.pandas_utils import sort_values
from zipline.utils.pool import SequentialPool
from zipline.utils.preprocess import preprocess

Expand Down Expand Up @@ -754,7 +753,7 @@ def overwrite_novel_deltas(baseline, deltas, dates):
ignore_index=True,
copy=False,
)
sort_values(cat, TS_FIELD_NAME, inplace=True)
cat.sort_values(TS_FIELD_NAME, inplace=True)
return cat, non_novel_deltas


Expand Down
3 changes: 1 addition & 2 deletions zipline/pipeline/loaders/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)
from zipline.lib.adjusted_array import AdjustedArray
from zipline.lib.adjustment import make_adjustment_from_labels
from zipline.utils.pandas_utils import sort_values
from .base import PipelineLoader

ADJUSTMENT_COLUMNS = Index([
Expand Down Expand Up @@ -72,7 +71,7 @@ def __init__(self, column, baseline, adjustments=None):
else:
# Ensure that columns are in the correct order.
adjustments = adjustments.reindex_axis(ADJUSTMENT_COLUMNS, axis=1)
sort_values(adjustments, ['apply_date', 'sid'], inplace=True)
adjustments.sort_values(['apply_date', 'sid'], inplace=True)

self.adjustments = adjustments
self.adjustment_apply_dates = DatetimeIndex(adjustments.apply_date)
Expand Down
99 changes: 4 additions & 95 deletions zipline/utils/pandas_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
pandas_version = StrictVersion(pd.__version__)


def july_5th_holiday_observance(datetime_index):
return datetime_index[datetime_index.year != 2013]


def explode(df):
"""
Take a DataFrame and return a triple of
Expand All @@ -21,19 +25,6 @@ def explode(df):
return df.index, df.columns, df.values


try:
# This branch is hit in pandas 17
sort_values = pd.DataFrame.sort_values
except AttributeError:
# This branch is hit in pandas 16
sort_values = pd.DataFrame.sort

if pandas_version >= StrictVersion('0.17.1'):
july_5th_holiday_observance = lambda dtix: dtix[dtix.year != 2013]
else:
july_5th_holiday_observance = lambda dt: None if dt.year == 2013 else dt


def _time_to_micros(time):
"""Convert a time into microseconds since midnight.
Parameters
Expand Down Expand Up @@ -125,85 +116,3 @@ def ignore_pandas_nan_categorical_warning():
category=FutureWarning,
)
yield


# Remove when we drop support for 0.17
if pandas_version >= StrictVersion('0.18'):
def rolling_mean(arg,
window,
min_periods=None,
freq=None,
center=False,
**kwargs):
return arg.rolling(
window,
min_periods=min_periods,
freq=freq,
center=center,
**kwargs
).mean()

def rolling_apply(arg,
window,
func,
min_periods=None,
freq=None,
center=False,
**kwargs):
return arg.rolling(
window,
min_periods=min_periods,
freq=freq,
center=center,
**kwargs
).apply(func)

def ewma(arg,
com=None,
span=None,
halflife=None,
alpha=None,
min_periods=0,
freq=None,
adjust=True,
how=None,
ignore_na=False):

return arg.ewm(
com=com,
span=span,
halflife=halflife,
alpha=alpha,
min_periods=min_periods,
freq=freq,
adjust=adjust,
ignore_na=ignore_na,
).mean()

def ewmstd(arg,
com=None,
span=None,
halflife=None,
alpha=None,
min_periods=0,
freq=None,
adjust=True,
how=None,
ignore_na=False):

return arg.ewm(
com=com,
span=span,
halflife=halflife,
alpha=alpha,
min_periods=min_periods,
freq=freq,
adjust=adjust,
ignore_na=ignore_na,
).std()

else:
rolling_mean = pd.rolling_mean
rolling_apply = pd.rolling_apply
ewma = pd.ewma
ewmstd = pd.ewmstd

0 comments on commit 03efb1c

Please sign in to comment.