|
| 1 | +import pytest |
| 2 | + |
| 3 | +from numpy.random import randn |
| 4 | +import numpy as np |
| 5 | +from datetime import datetime |
| 6 | +import pandas as pd |
| 7 | +from pandas import (Series, DataFrame, bdate_range, |
| 8 | + notna) |
| 9 | +import pandas.core.window as rwindow |
| 10 | +from pandas.errors import UnsupportedFunctionCall |
| 11 | +import pandas.util.testing as tm |
| 12 | + |
| 13 | +N, K = 100, 10 |
| 14 | + |
| 15 | + |
| 16 | +def assert_equal(left, right): |
| 17 | + if isinstance(left, Series): |
| 18 | + tm.assert_series_equal(left, right) |
| 19 | + else: |
| 20 | + tm.assert_frame_equal(left, right) |
| 21 | + |
| 22 | + |
| 23 | +class Base(object): |
| 24 | + |
| 25 | + _nan_locs = np.arange(20, 40) |
| 26 | + _inf_locs = np.array([]) |
| 27 | + |
| 28 | + def _create_data(self): |
| 29 | + arr = randn(N) |
| 30 | + arr[self._nan_locs] = np.NaN |
| 31 | + |
| 32 | + self.arr = arr |
| 33 | + self.rng = bdate_range(datetime(2009, 1, 1), periods=N) |
| 34 | + self.series = Series(arr.copy(), index=self.rng) |
| 35 | + self.frame = DataFrame(randn(N, K), index=self.rng, |
| 36 | + columns=np.arange(K)) |
| 37 | + |
| 38 | + |
| 39 | +class TestExpanding(Base): |
| 40 | + |
| 41 | + def setup_method(self, method): |
| 42 | + self._create_data() |
| 43 | + |
| 44 | + def test_doc_string(self): |
| 45 | + |
| 46 | + df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) |
| 47 | + df |
| 48 | + df.expanding(2).sum() |
| 49 | + |
| 50 | + def test_constructor(self): |
| 51 | + # GH 12669 |
| 52 | + |
| 53 | + for o in [self.series, self.frame]: |
| 54 | + c = o.expanding |
| 55 | + |
| 56 | + # valid |
| 57 | + c(min_periods=1) |
| 58 | + c(min_periods=1, center=True) |
| 59 | + c(min_periods=1, center=False) |
| 60 | + |
| 61 | + # not valid |
| 62 | + for w in [2., 'foo', np.array([2])]: |
| 63 | + with pytest.raises(ValueError): |
| 64 | + c(min_periods=w) |
| 65 | + with pytest.raises(ValueError): |
| 66 | + c(min_periods=1, center=w) |
| 67 | + |
| 68 | + def test_numpy_compat(self): |
| 69 | + # see gh-12811 |
| 70 | + e = rwindow.Expanding(Series([2, 4, 6]), window=2) |
| 71 | + |
| 72 | + msg = "numpy operations are not valid with window objects" |
| 73 | + |
| 74 | + for func in ('std', 'mean', 'sum', 'max', 'min', 'var'): |
| 75 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 76 | + getattr(e, func), 1, 2, 3) |
| 77 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 78 | + getattr(e, func), dtype=np.float64) |
| 79 | + |
| 80 | + @pytest.mark.parametrize( |
| 81 | + 'expander', |
| 82 | + [1, pytest.param('ls', marks=pytest.mark.xfail( |
| 83 | + reason='GH 16425 expanding with ' |
| 84 | + 'offset not supported'))]) |
| 85 | + def test_empty_df_expanding(self, expander): |
| 86 | + # GH 15819 Verifies that datetime and integer expanding windows can be |
| 87 | + # applied to empty DataFrames |
| 88 | + |
| 89 | + expected = DataFrame() |
| 90 | + result = DataFrame().expanding(expander).sum() |
| 91 | + tm.assert_frame_equal(result, expected) |
| 92 | + |
| 93 | + # Verifies that datetime and integer expanding windows can be applied |
| 94 | + # to empty DataFrames with datetime index |
| 95 | + expected = DataFrame(index=pd.DatetimeIndex([])) |
| 96 | + result = DataFrame( |
| 97 | + index=pd.DatetimeIndex([])).expanding(expander).sum() |
| 98 | + tm.assert_frame_equal(result, expected) |
| 99 | + |
| 100 | + def test_missing_minp_zero(self): |
| 101 | + # https://github.com/pandas-dev/pandas/pull/18921 |
| 102 | + # minp=0 |
| 103 | + x = pd.Series([np.nan]) |
| 104 | + result = x.expanding(min_periods=0).sum() |
| 105 | + expected = pd.Series([0.0]) |
| 106 | + tm.assert_series_equal(result, expected) |
| 107 | + |
| 108 | + # minp=1 |
| 109 | + result = x.expanding(min_periods=1).sum() |
| 110 | + expected = pd.Series([np.nan]) |
| 111 | + tm.assert_series_equal(result, expected) |
| 112 | + |
| 113 | + |
| 114 | +# create the data only once as we are not setting it |
| 115 | +def _create_consistency_data(): |
| 116 | + def create_series(): |
| 117 | + return [Series(), |
| 118 | + Series([np.nan]), |
| 119 | + Series([np.nan, np.nan]), |
| 120 | + Series([3.]), |
| 121 | + Series([np.nan, 3.]), |
| 122 | + Series([3., np.nan]), |
| 123 | + Series([1., 3.]), |
| 124 | + Series([2., 2.]), |
| 125 | + Series([3., 1.]), |
| 126 | + Series([5., 5., 5., 5., np.nan, np.nan, np.nan, 5., 5., np.nan, |
| 127 | + np.nan]), |
| 128 | + Series([np.nan, 5., 5., 5., np.nan, np.nan, np.nan, 5., 5., |
| 129 | + np.nan, np.nan]), |
| 130 | + Series([np.nan, np.nan, 5., 5., np.nan, np.nan, np.nan, 5., 5., |
| 131 | + np.nan, np.nan]), |
| 132 | + Series([np.nan, 3., np.nan, 3., 4., 5., 6., np.nan, np.nan, 7., |
| 133 | + 12., 13., 14., 15.]), |
| 134 | + Series([np.nan, 5., np.nan, 2., 4., 0., 9., np.nan, np.nan, 3., |
| 135 | + 12., 13., 14., 15.]), |
| 136 | + Series([2., 3., np.nan, 3., 4., 5., 6., np.nan, np.nan, 7., |
| 137 | + 12., 13., 14., 15.]), |
| 138 | + Series([2., 5., np.nan, 2., 4., 0., 9., np.nan, np.nan, 3., |
| 139 | + 12., 13., 14., 15.]), |
| 140 | + Series(range(10)), |
| 141 | + Series(range(20, 0, -2)), ] |
| 142 | + |
| 143 | + def create_dataframes(): |
| 144 | + return ([DataFrame(), |
| 145 | + DataFrame(columns=['a']), |
| 146 | + DataFrame(columns=['a', 'a']), |
| 147 | + DataFrame(columns=['a', 'b']), |
| 148 | + DataFrame(np.arange(10).reshape((5, 2))), |
| 149 | + DataFrame(np.arange(25).reshape((5, 5))), |
| 150 | + DataFrame(np.arange(25).reshape((5, 5)), |
| 151 | + columns=['a', 'b', 99, 'd', 'd'])] + |
| 152 | + [DataFrame(s) for s in create_series()]) |
| 153 | + |
| 154 | + def is_constant(x): |
| 155 | + values = x.values.ravel() |
| 156 | + return len(set(values[notna(values)])) == 1 |
| 157 | + |
| 158 | + def no_nans(x): |
| 159 | + return x.notna().all().all() |
| 160 | + |
| 161 | + # data is a tuple(object, is_contant, no_nans) |
| 162 | + data = create_series() + create_dataframes() |
| 163 | + |
| 164 | + return [(x, is_constant(x), no_nans(x)) for x in data] |
| 165 | + |
| 166 | + |
| 167 | +_consistency_data = _create_consistency_data() |
| 168 | + |
| 169 | + |
| 170 | +class TestGrouperGrouping(object): |
| 171 | + |
| 172 | + def setup_method(self, method): |
| 173 | + self.series = Series(np.arange(10)) |
| 174 | + self.frame = DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8, |
| 175 | + 'B': np.arange(40)}) |
| 176 | + |
| 177 | + def test_mutated(self): |
| 178 | + |
| 179 | + def f(): |
| 180 | + self.frame.groupby('A', foo=1) |
| 181 | + pytest.raises(TypeError, f) |
| 182 | + |
| 183 | + g = self.frame.groupby('A') |
| 184 | + assert not g.mutated |
| 185 | + g = self.frame.groupby('A', mutated=True) |
| 186 | + assert g.mutated |
| 187 | + |
| 188 | + def test_getitem(self): |
| 189 | + g = self.frame.groupby('A') |
| 190 | + g_mutated = self.frame.groupby('A', mutated=True) |
| 191 | + |
| 192 | + expected = g_mutated.B.apply(lambda x: x.rolling(2).mean()) |
| 193 | + |
| 194 | + result = g.rolling(2).mean().B |
| 195 | + tm.assert_series_equal(result, expected) |
| 196 | + |
| 197 | + result = g.rolling(2).B.mean() |
| 198 | + tm.assert_series_equal(result, expected) |
| 199 | + |
| 200 | + result = g.B.rolling(2).mean() |
| 201 | + tm.assert_series_equal(result, expected) |
| 202 | + |
| 203 | + result = self.frame.B.groupby(self.frame.A).rolling(2).mean() |
| 204 | + tm.assert_series_equal(result, expected) |
| 205 | + |
| 206 | + def test_getitem_multiple(self): |
| 207 | + |
| 208 | + # GH 13174 |
| 209 | + g = self.frame.groupby('A') |
| 210 | + r = g.rolling(2) |
| 211 | + g_mutated = self.frame.groupby('A', mutated=True) |
| 212 | + expected = g_mutated.B.apply(lambda x: x.rolling(2).count()) |
| 213 | + |
| 214 | + result = r.B.count() |
| 215 | + tm.assert_series_equal(result, expected) |
| 216 | + |
| 217 | + result = r.B.count() |
| 218 | + tm.assert_series_equal(result, expected) |
| 219 | + |
| 220 | + def test_expanding(self): |
| 221 | + g = self.frame.groupby('A') |
| 222 | + r = g.expanding() |
| 223 | + |
| 224 | + for f in ['sum', 'mean', 'min', 'max', 'count', 'kurt', 'skew']: |
| 225 | + |
| 226 | + result = getattr(r, f)() |
| 227 | + expected = g.apply(lambda x: getattr(x.expanding(), f)()) |
| 228 | + tm.assert_frame_equal(result, expected) |
| 229 | + |
| 230 | + for f in ['std', 'var']: |
| 231 | + result = getattr(r, f)(ddof=0) |
| 232 | + expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) |
| 233 | + tm.assert_frame_equal(result, expected) |
| 234 | + |
| 235 | + result = r.quantile(0.5) |
| 236 | + expected = g.apply(lambda x: x.expanding().quantile(0.5)) |
| 237 | + tm.assert_frame_equal(result, expected) |
| 238 | + |
| 239 | + def test_expanding_corr_cov(self): |
| 240 | + g = self.frame.groupby('A') |
| 241 | + r = g.expanding() |
| 242 | + |
| 243 | + for f in ['corr', 'cov']: |
| 244 | + result = getattr(r, f)(self.frame) |
| 245 | + |
| 246 | + def func(x): |
| 247 | + return getattr(x.expanding(), f)(self.frame) |
| 248 | + expected = g.apply(func) |
| 249 | + tm.assert_frame_equal(result, expected) |
| 250 | + |
| 251 | + result = getattr(r.B, f)(pairwise=True) |
| 252 | + |
| 253 | + def func(x): |
| 254 | + return getattr(x.B.expanding(), f)(pairwise=True) |
| 255 | + expected = g.apply(func) |
| 256 | + tm.assert_series_equal(result, expected) |
| 257 | + |
| 258 | + def test_expanding_apply(self): |
| 259 | + g = self.frame.groupby('A') |
| 260 | + r = g.expanding() |
| 261 | + |
| 262 | + # reduction |
| 263 | + result = r.apply(lambda x: x.sum()) |
| 264 | + expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum())) |
| 265 | + tm.assert_frame_equal(result, expected) |
0 commit comments