|
1 | 1 | # pylint: disable=E1101
|
2 | 2 |
|
3 | 3 | from datetime import datetime, timedelta
|
| 4 | +from functools import partial |
4 | 5 |
|
5 | 6 | from pandas.compat import range, lrange, zip, product
|
6 | 7 | import numpy as np
|
@@ -140,6 +141,30 @@ def _ohlc(group):
|
140 | 141 | exc.args += ('how=%s' % arg,)
|
141 | 142 | raise
|
142 | 143 |
|
| 144 | + def test_resample_how_callables(self): |
| 145 | + # GH 7929 |
| 146 | + data = np.arange(5, dtype=np.int64) |
| 147 | + ind = pd.DatetimeIndex(start='2014-01-01', periods=len(data), freq='d') |
| 148 | + df = pd.DataFrame({"A": data, "B": data}, index=ind) |
| 149 | + |
| 150 | + def fn(x, a=1): |
| 151 | + return str(type(x)) |
| 152 | + |
| 153 | + class fn_class: |
| 154 | + def __call__(self, x): |
| 155 | + return str(type(x)) |
| 156 | + |
| 157 | + df_standard = df.resample("M", how=fn) |
| 158 | + df_lambda = df.resample("M", how=lambda x: str(type(x))) |
| 159 | + df_partial = df.resample("M", how=partial(fn)) |
| 160 | + df_partial2 = df.resample("M", how=partial(fn, a=2)) |
| 161 | + df_class = df.resample("M", how=fn_class()) |
| 162 | + |
| 163 | + assert_frame_equal(df_standard, df_lambda) |
| 164 | + assert_frame_equal(df_standard, df_partial) |
| 165 | + assert_frame_equal(df_standard, df_partial2) |
| 166 | + assert_frame_equal(df_standard, df_class) |
| 167 | + |
143 | 168 | def test_resample_basic_from_daily(self):
|
144 | 169 | # from daily
|
145 | 170 | dti = DatetimeIndex(
|
@@ -765,6 +790,7 @@ def test_resample_timegrouper(self):
|
765 | 790 | assert_frame_equal(result, expected)
|
766 | 791 |
|
767 | 792 |
|
| 793 | + |
768 | 794 | def _simple_ts(start, end, freq='D'):
|
769 | 795 | rng = date_range(start, end, freq=freq)
|
770 | 796 | return Series(np.random.randn(len(rng)), index=rng)
|
|
0 commit comments