Skip to content

Commit e430ffc

Browse files
mroeschkeKevin D Smith
authored and
Kevin D Smith
committed
CLN: moments/test_moments_rolling.py for apply (pandas-dev#36676)
1 parent cdf6746 commit e430ffc

File tree

2 files changed

+151
-19
lines changed

2 files changed

+151
-19
lines changed

pandas/tests/window/moments/test_moments_rolling.py

-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
import warnings
32

43
import numpy as np
54
from numpy.random import randn
@@ -856,24 +855,6 @@ def test_rolling_quantile_param():
856855
ser.rolling(3).quantile("foo")
857856

858857

859-
def test_rolling_apply(raw, series, frame):
860-
# suppress warnings about empty slices, as we are deliberately testing
861-
# with a 0-length Series
862-
863-
def f(x):
864-
with warnings.catch_warnings():
865-
warnings.filterwarnings(
866-
"ignore",
867-
message=".*(empty slice|0 for slice).*",
868-
category=RuntimeWarning,
869-
)
870-
return x[np.isfinite(x)].mean()
871-
872-
_check_moment_func(
873-
np.mean, name="apply", func=f, raw=raw, series=series, frame=frame
874-
)
875-
876-
877858
def test_rolling_std(raw, series, frame):
878859
_check_moment_func(
879860
lambda x: np.std(x, ddof=1), name="std", raw=raw, series=series, frame=frame
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import warnings
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import DataFrame, Series, concat, isna, notna
7+
import pandas._testing as tm
8+
9+
import pandas.tseries.offsets as offsets
10+
11+
12+
def f(x):
13+
# suppress warnings about empty slices, as we are deliberately testing
14+
# with a 0-length Series
15+
with warnings.catch_warnings():
16+
warnings.filterwarnings(
17+
"ignore",
18+
message=".*(empty slice|0 for slice).*",
19+
category=RuntimeWarning,
20+
)
21+
return x[np.isfinite(x)].mean()
22+
23+
24+
def test_series(raw, series):
25+
result = series.rolling(50).apply(f, raw=raw)
26+
assert isinstance(result, Series)
27+
tm.assert_almost_equal(result.iloc[-1], np.mean(series[-50:]))
28+
29+
30+
def test_frame(raw, frame):
31+
result = frame.rolling(50).apply(f, raw=raw)
32+
assert isinstance(result, DataFrame)
33+
tm.assert_series_equal(
34+
result.iloc[-1, :],
35+
frame.iloc[-50:, :].apply(np.mean, axis=0, raw=raw),
36+
check_names=False,
37+
)
38+
39+
40+
def test_time_rule_series(raw, series):
41+
win = 25
42+
minp = 10
43+
ser = series[::2].resample("B").mean()
44+
series_result = ser.rolling(window=win, min_periods=minp).apply(f, raw=raw)
45+
last_date = series_result.index[-1]
46+
prev_date = last_date - 24 * offsets.BDay()
47+
48+
trunc_series = series[::2].truncate(prev_date, last_date)
49+
tm.assert_almost_equal(series_result[-1], np.mean(trunc_series))
50+
51+
52+
def test_time_rule_frame(raw, frame):
53+
win = 25
54+
minp = 10
55+
frm = frame[::2].resample("B").mean()
56+
frame_result = frm.rolling(window=win, min_periods=minp).apply(f, raw=raw)
57+
last_date = frame_result.index[-1]
58+
prev_date = last_date - 24 * offsets.BDay()
59+
60+
trunc_frame = frame[::2].truncate(prev_date, last_date)
61+
tm.assert_series_equal(
62+
frame_result.xs(last_date),
63+
trunc_frame.apply(np.mean, raw=raw),
64+
check_names=False,
65+
)
66+
67+
68+
def test_nans(raw):
69+
obj = Series(np.random.randn(50))
70+
obj[:10] = np.NaN
71+
obj[-10:] = np.NaN
72+
73+
result = obj.rolling(50, min_periods=30).apply(f, raw=raw)
74+
tm.assert_almost_equal(result.iloc[-1], np.mean(obj[10:-10]))
75+
76+
# min_periods is working correctly
77+
result = obj.rolling(20, min_periods=15).apply(f, raw=raw)
78+
assert isna(result.iloc[23])
79+
assert not isna(result.iloc[24])
80+
81+
assert not isna(result.iloc[-6])
82+
assert isna(result.iloc[-5])
83+
84+
obj2 = Series(np.random.randn(20))
85+
result = obj2.rolling(10, min_periods=5).apply(f, raw=raw)
86+
assert isna(result.iloc[3])
87+
assert notna(result.iloc[4])
88+
89+
result0 = obj.rolling(20, min_periods=0).apply(f, raw=raw)
90+
result1 = obj.rolling(20, min_periods=1).apply(f, raw=raw)
91+
tm.assert_almost_equal(result0, result1)
92+
93+
94+
@pytest.mark.parametrize("minp", [0, 99, 100])
95+
def test_min_periods(raw, series, minp):
96+
result = series.rolling(len(series) + 1, min_periods=minp).apply(f, raw=raw)
97+
expected = series.rolling(len(series), min_periods=minp).apply(f, raw=raw)
98+
nan_mask = isna(result)
99+
tm.assert_series_equal(nan_mask, isna(expected))
100+
101+
nan_mask = ~nan_mask
102+
tm.assert_almost_equal(result[nan_mask], expected[nan_mask])
103+
104+
105+
def test_center(raw):
106+
obj = Series(np.random.randn(50))
107+
obj[:10] = np.NaN
108+
obj[-10:] = np.NaN
109+
110+
result = obj.rolling(20, min_periods=15, center=True).apply(f, raw=raw)
111+
expected = (
112+
concat([obj, Series([np.NaN] * 9)])
113+
.rolling(20, min_periods=15)
114+
.apply(f, raw=raw)[9:]
115+
.reset_index(drop=True)
116+
)
117+
tm.assert_series_equal(result, expected)
118+
119+
120+
def test_center_reindex_series(raw, series):
121+
# shifter index
122+
s = [f"x{x:d}" for x in range(12)]
123+
minp = 10
124+
125+
series_xp = (
126+
series.reindex(list(series.index) + s)
127+
.rolling(window=25, min_periods=minp)
128+
.apply(f, raw=raw)
129+
.shift(-12)
130+
.reindex(series.index)
131+
)
132+
series_rs = series.rolling(window=25, min_periods=minp, center=True).apply(
133+
f, raw=raw
134+
)
135+
tm.assert_series_equal(series_xp, series_rs)
136+
137+
138+
def test_center_reindex_frame(raw, frame):
139+
# shifter index
140+
s = [f"x{x:d}" for x in range(12)]
141+
minp = 10
142+
143+
frame_xp = (
144+
frame.reindex(list(frame.index) + s)
145+
.rolling(window=25, min_periods=minp)
146+
.apply(f, raw=raw)
147+
.shift(-12)
148+
.reindex(frame.index)
149+
)
150+
frame_rs = frame.rolling(window=25, min_periods=minp, center=True).apply(f, raw=raw)
151+
tm.assert_frame_equal(frame_xp, frame_rs)

0 commit comments

Comments
 (0)