|
1 | 1 | import numpy as np
|
2 |
| -from numpy.random import randn |
3 | 2 | import pytest
|
4 | 3 |
|
5 | 4 | import pandas.util._test_decorators as td
|
6 | 5 |
|
7 | 6 | import pandas as pd
|
8 |
| -from pandas import DataFrame, Series, isna, notna |
| 7 | +from pandas import DataFrame, Series |
9 | 8 | import pandas._testing as tm
|
10 | 9 |
|
11 |
| -import pandas.tseries.offsets as offsets |
12 |
| - |
13 |
| - |
14 |
| -def _check_moment_func( |
15 |
| - static_comp, |
16 |
| - name, |
17 |
| - raw, |
18 |
| - has_min_periods=True, |
19 |
| - has_center=True, |
20 |
| - has_time_rule=True, |
21 |
| - fill_value=None, |
22 |
| - zero_min_periods_equal=True, |
23 |
| - series=None, |
24 |
| - frame=None, |
25 |
| - **kwargs, |
26 |
| -): |
27 |
| - def get_result(obj, window, min_periods=None, center=False): |
28 |
| - r = obj.rolling(window=window, min_periods=min_periods, center=center) |
29 |
| - return getattr(r, name)(**kwargs) |
30 |
| - |
31 |
| - series_result = get_result(series, window=50) |
32 |
| - assert isinstance(series_result, Series) |
33 |
| - tm.assert_almost_equal(series_result.iloc[-1], static_comp(series[-50:])) |
34 |
| - |
35 |
| - frame_result = get_result(frame, window=50) |
36 |
| - assert isinstance(frame_result, DataFrame) |
37 |
| - tm.assert_series_equal( |
38 |
| - frame_result.iloc[-1, :], |
39 |
| - frame.iloc[-50:, :].apply(static_comp, axis=0, raw=raw), |
40 |
| - check_names=False, |
41 |
| - ) |
42 |
| - |
43 |
| - # check time_rule works |
44 |
| - if has_time_rule: |
45 |
| - win = 25 |
46 |
| - minp = 10 |
47 |
| - ser = series[::2].resample("B").mean() |
48 |
| - frm = frame[::2].resample("B").mean() |
49 |
| - |
50 |
| - if has_min_periods: |
51 |
| - series_result = get_result(ser, window=win, min_periods=minp) |
52 |
| - frame_result = get_result(frm, window=win, min_periods=minp) |
53 |
| - else: |
54 |
| - series_result = get_result(ser, window=win, min_periods=0) |
55 |
| - frame_result = get_result(frm, window=win, min_periods=0) |
56 |
| - |
57 |
| - last_date = series_result.index[-1] |
58 |
| - prev_date = last_date - 24 * offsets.BDay() |
59 |
| - |
60 |
| - trunc_series = series[::2].truncate(prev_date, last_date) |
61 |
| - trunc_frame = frame[::2].truncate(prev_date, last_date) |
62 |
| - |
63 |
| - tm.assert_almost_equal(series_result[-1], static_comp(trunc_series)) |
64 |
| - |
65 |
| - tm.assert_series_equal( |
66 |
| - frame_result.xs(last_date), |
67 |
| - trunc_frame.apply(static_comp, raw=raw), |
68 |
| - check_names=False, |
69 |
| - ) |
70 |
| - |
71 |
| - # excluding NaNs correctly |
72 |
| - obj = Series(randn(50)) |
73 |
| - obj[:10] = np.NaN |
74 |
| - obj[-10:] = np.NaN |
75 |
| - if has_min_periods: |
76 |
| - result = get_result(obj, 50, min_periods=30) |
77 |
| - tm.assert_almost_equal(result.iloc[-1], static_comp(obj[10:-10])) |
78 |
| - |
79 |
| - # min_periods is working correctly |
80 |
| - result = get_result(obj, 20, min_periods=15) |
81 |
| - assert isna(result.iloc[23]) |
82 |
| - assert not isna(result.iloc[24]) |
83 |
| - |
84 |
| - assert not isna(result.iloc[-6]) |
85 |
| - assert isna(result.iloc[-5]) |
86 |
| - |
87 |
| - obj2 = Series(randn(20)) |
88 |
| - result = get_result(obj2, 10, min_periods=5) |
89 |
| - assert isna(result.iloc[3]) |
90 |
| - assert notna(result.iloc[4]) |
91 |
| - |
92 |
| - if zero_min_periods_equal: |
93 |
| - # min_periods=0 may be equivalent to min_periods=1 |
94 |
| - result0 = get_result(obj, 20, min_periods=0) |
95 |
| - result1 = get_result(obj, 20, min_periods=1) |
96 |
| - tm.assert_almost_equal(result0, result1) |
97 |
| - else: |
98 |
| - result = get_result(obj, 50) |
99 |
| - tm.assert_almost_equal(result.iloc[-1], static_comp(obj[10:-10])) |
100 |
| - |
101 |
| - # window larger than series length (#7297) |
102 |
| - if has_min_periods: |
103 |
| - for minp in (0, len(series) - 1, len(series)): |
104 |
| - result = get_result(series, len(series) + 1, min_periods=minp) |
105 |
| - expected = get_result(series, len(series), min_periods=minp) |
106 |
| - nan_mask = isna(result) |
107 |
| - tm.assert_series_equal(nan_mask, isna(expected)) |
108 |
| - |
109 |
| - nan_mask = ~nan_mask |
110 |
| - tm.assert_almost_equal(result[nan_mask], expected[nan_mask]) |
111 |
| - else: |
112 |
| - result = get_result(series, len(series) + 1, min_periods=0) |
113 |
| - expected = get_result(series, len(series), min_periods=0) |
114 |
| - nan_mask = isna(result) |
115 |
| - tm.assert_series_equal(nan_mask, isna(expected)) |
116 |
| - |
117 |
| - nan_mask = ~nan_mask |
118 |
| - tm.assert_almost_equal(result[nan_mask], expected[nan_mask]) |
119 |
| - |
120 |
| - # check center=True |
121 |
| - if has_center: |
122 |
| - if has_min_periods: |
123 |
| - result = get_result(obj, 20, min_periods=15, center=True) |
124 |
| - expected = get_result( |
125 |
| - pd.concat([obj, Series([np.NaN] * 9)]), 20, min_periods=15 |
126 |
| - )[9:].reset_index(drop=True) |
127 |
| - else: |
128 |
| - result = get_result(obj, 20, min_periods=0, center=True) |
129 |
| - print(result) |
130 |
| - expected = get_result( |
131 |
| - pd.concat([obj, Series([np.NaN] * 9)]), 20, min_periods=0 |
132 |
| - )[9:].reset_index(drop=True) |
133 |
| - |
134 |
| - tm.assert_series_equal(result, expected) |
135 |
| - |
136 |
| - # shifter index |
137 |
| - s = [f"x{x:d}" for x in range(12)] |
138 |
| - |
139 |
| - if has_min_periods: |
140 |
| - minp = 10 |
141 |
| - |
142 |
| - series_xp = ( |
143 |
| - get_result( |
144 |
| - series.reindex(list(series.index) + s), window=25, min_periods=minp |
145 |
| - ) |
146 |
| - .shift(-12) |
147 |
| - .reindex(series.index) |
148 |
| - ) |
149 |
| - frame_xp = ( |
150 |
| - get_result( |
151 |
| - frame.reindex(list(frame.index) + s), window=25, min_periods=minp |
152 |
| - ) |
153 |
| - .shift(-12) |
154 |
| - .reindex(frame.index) |
155 |
| - ) |
156 |
| - |
157 |
| - series_rs = get_result(series, window=25, min_periods=minp, center=True) |
158 |
| - frame_rs = get_result(frame, window=25, min_periods=minp, center=True) |
159 |
| - |
160 |
| - else: |
161 |
| - series_xp = ( |
162 |
| - get_result( |
163 |
| - series.reindex(list(series.index) + s), window=25, min_periods=0 |
164 |
| - ) |
165 |
| - .shift(-12) |
166 |
| - .reindex(series.index) |
167 |
| - ) |
168 |
| - frame_xp = ( |
169 |
| - get_result( |
170 |
| - frame.reindex(list(frame.index) + s), window=25, min_periods=0 |
171 |
| - ) |
172 |
| - .shift(-12) |
173 |
| - .reindex(frame.index) |
174 |
| - ) |
175 |
| - |
176 |
| - series_rs = get_result(series, window=25, min_periods=0, center=True) |
177 |
| - frame_rs = get_result(frame, window=25, min_periods=0, center=True) |
178 |
| - |
179 |
| - if fill_value is not None: |
180 |
| - series_xp = series_xp.fillna(fill_value) |
181 |
| - frame_xp = frame_xp.fillna(fill_value) |
182 |
| - tm.assert_series_equal(series_xp, series_rs) |
183 |
| - tm.assert_frame_equal(frame_xp, frame_rs) |
184 |
| - |
185 | 10 |
|
186 | 11 | def test_centered_axis_validation():
|
187 | 12 |
|
@@ -716,33 +541,6 @@ def test_rolling_max_min_periods():
|
716 | 541 | pd.Series([1, 2, 3]).rolling(window=3, min_periods=5).max()
|
717 | 542 |
|
718 | 543 |
|
719 |
| -@pytest.mark.parametrize("q", [0.0, 0.1, 0.5, 0.9, 1.0]) |
720 |
| -def test_rolling_quantile(q, raw, series, frame): |
721 |
| - def scoreatpercentile(a, per): |
722 |
| - values = np.sort(a, axis=0) |
723 |
| - |
724 |
| - idx = int(per / 1.0 * (values.shape[0] - 1)) |
725 |
| - |
726 |
| - if idx == values.shape[0] - 1: |
727 |
| - retval = values[-1] |
728 |
| - |
729 |
| - else: |
730 |
| - qlow = float(idx) / float(values.shape[0] - 1) |
731 |
| - qhig = float(idx + 1) / float(values.shape[0] - 1) |
732 |
| - vlow = values[idx] |
733 |
| - vhig = values[idx + 1] |
734 |
| - retval = vlow + (vhig - vlow) * (per - qlow) / (qhig - qlow) |
735 |
| - |
736 |
| - return retval |
737 |
| - |
738 |
| - def quantile_func(x): |
739 |
| - return scoreatpercentile(x, q) |
740 |
| - |
741 |
| - _check_moment_func( |
742 |
| - quantile_func, name="quantile", quantile=q, raw=raw, series=series, frame=frame |
743 |
| - ) |
744 |
| - |
745 |
| - |
746 | 544 | def test_rolling_quantile_np_percentile():
|
747 | 545 | # #9413: Tests that rolling window's quantile default behavior
|
748 | 546 | # is analogous to Numpy's percentile
|
@@ -845,25 +643,3 @@ def test_rolling_std_neg_sqrt():
|
845 | 643 |
|
846 | 644 | b = a.ewm(span=3).std()
|
847 | 645 | assert np.isfinite(b[2:]).all()
|
848 |
| - |
849 |
| - |
850 |
| -@td.skip_if_no_scipy |
851 |
| -def test_rolling_skew(raw, series, frame): |
852 |
| - from scipy.stats import skew |
853 |
| - |
854 |
| - _check_moment_func( |
855 |
| - lambda x: skew(x, bias=False), name="skew", raw=raw, series=series, frame=frame |
856 |
| - ) |
857 |
| - |
858 |
| - |
859 |
| -@td.skip_if_no_scipy |
860 |
| -def test_rolling_kurt(raw, series, frame): |
861 |
| - from scipy.stats import kurtosis |
862 |
| - |
863 |
| - _check_moment_func( |
864 |
| - lambda x: kurtosis(x, bias=False), |
865 |
| - name="kurt", |
866 |
| - raw=raw, |
867 |
| - series=series, |
868 |
| - frame=frame, |
869 |
| - ) |
0 commit comments