Skip to content

Commit 045e47a

Browse files
author
tp
committed
added test_expanding_func and test_expanding_apply
1 parent eccc3c9 commit 045e47a

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

pandas/tests/test_window.py

+69-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import pandas as pd
1111
from pandas import (Series, DataFrame, bdate_range,
12-
notna, concat, Timestamp, Index)
12+
isna, notna, concat, Timestamp, Index)
1313
import pandas.core.window as rwindow
1414
import pandas.tseries.offsets as offsets
1515
from pandas.core.base import SpecificationError
@@ -2334,16 +2334,6 @@ def func(A, B, com, **kwargs):
23342334

23352335
pytest.raises(Exception, func, A, randn(50), 20, min_periods=5)
23362336

2337-
def test_expanding_apply(self):
2338-
ser = Series([])
2339-
tm.assert_series_equal(ser, ser.expanding().apply(lambda x: x.mean()))
2340-
2341-
# GH 8080
2342-
s = Series([None, None, None])
2343-
result = s.expanding(min_periods=0).apply(lambda x: len(x))
2344-
expected = Series([1., 2., 3.])
2345-
tm.assert_series_equal(result, expected)
2346-
23472337
def test_expanding_apply_args_kwargs(self):
23482338
def mean_w_arg(x, const):
23492339
return np.mean(x) + const
@@ -2720,6 +2710,74 @@ def test_rolling_kurt_eq_value_fperr(self):
27202710
a = Series([1.1] * 15).rolling(window=10).kurt()
27212711
assert np.isnan(a).all()
27222712

2713+
@pytest.mark.parametrize('func,static_comp', [('sum', np.sum),
2714+
('mean', np.mean),
2715+
('max', np.max),
2716+
('min', np.min)],
2717+
ids=['sum', 'mean', 'max', 'min'])
2718+
def test_expanding_func(self, func, static_comp):
2719+
def expanding_func(x, min_periods=1, center=False, axis=0):
2720+
exp = x.expanding(min_periods=min_periods,
2721+
center=center, axis=axis)
2722+
return getattr(exp, func)()
2723+
self._check_expanding(expanding_func, static_comp, preserve_nan=False)
2724+
2725+
def test_expanding_apply(self):
2726+
2727+
def expanding_mean(x, min_periods=1):
2728+
exp = x.expanding(min_periods=min_periods)
2729+
return exp.apply(lambda x: x.mean())
2730+
2731+
self._check_expanding(expanding_mean, np.mean)
2732+
2733+
ser = Series([])
2734+
tm.assert_series_equal(ser, ser.expanding().apply(lambda x: x.mean()))
2735+
2736+
# GH 8080
2737+
s = Series([None, None, None])
2738+
result = s.expanding(min_periods=0).apply(lambda x: len(x))
2739+
expected = Series([1., 2., 3.])
2740+
tm.assert_series_equal(result, expected)
2741+
2742+
def _check_expanding(self, func, static_comp, has_min_periods=True,
2743+
has_time_rule=True, preserve_nan=True):
2744+
2745+
series_result = func(self.series)
2746+
assert isinstance(series_result, Series)
2747+
frame_result = func(self.frame)
2748+
assert isinstance(frame_result, DataFrame)
2749+
2750+
result = func(self.series)
2751+
tm.assert_almost_equal(result[10], static_comp(self.series[:11]))
2752+
2753+
if preserve_nan:
2754+
assert result.iloc[self._nan_locs].isna().all()
2755+
2756+
ser = Series(randn(50))
2757+
2758+
if has_min_periods:
2759+
result = func(ser, min_periods=30)
2760+
assert result[:29].isna().all()
2761+
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
2762+
2763+
# min_periods is working correctly
2764+
result = func(ser, min_periods=15)
2765+
assert isna(result.iloc[13])
2766+
assert notna(result.iloc[14])
2767+
2768+
ser2 = Series(randn(20))
2769+
result = func(ser2, min_periods=5)
2770+
assert isna(result[3])
2771+
assert notna(result[4])
2772+
2773+
# min_periods=0
2774+
result0 = func(ser, min_periods=0)
2775+
result1 = func(ser, min_periods=1)
2776+
tm.assert_almost_equal(result0, result1)
2777+
else:
2778+
result = func(ser)
2779+
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
2780+
27232781
def test_rolling_max_gh6297(self):
27242782
"""Replicate result expected in GH #6297"""
27252783

0 commit comments

Comments
 (0)