Skip to content

Commit dbe2b49

Browse files
authored
TST: Split / parameterize resample pt2 (#45282)
1 parent 6069132 commit dbe2b49

File tree

3 files changed

+82
-59
lines changed

3 files changed

+82
-59
lines changed

pandas/tests/resample/test_resample_api.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,21 @@ def test_attribute_access(test_frame):
142142
tm.assert_series_equal(r.A.sum(), r["A"].sum())
143143

144144

145-
def test_api_compat_before_use():
145+
@pytest.mark.parametrize("attr", ["groups", "ngroups", "indices"])
146+
def test_api_compat_before_use(attr):
146147

147148
# make sure that we are setting the binner
148149
# on these attributes
149-
for attr in ["groups", "ngroups", "indices"]:
150-
rng = date_range("1/1/2012", periods=100, freq="S")
151-
ts = Series(np.arange(len(rng)), index=rng)
152-
rs = ts.resample("30s")
150+
rng = date_range("1/1/2012", periods=100, freq="S")
151+
ts = Series(np.arange(len(rng)), index=rng)
152+
rs = ts.resample("30s")
153153

154-
# before use
155-
getattr(rs, attr)
154+
# before use
155+
getattr(rs, attr)
156156

157-
# after grouper is initialized is ok
158-
rs.mean()
159-
getattr(rs, attr)
157+
# after grouper is initialized is ok
158+
rs.mean()
159+
getattr(rs, attr)
160160

161161

162162
def tests_skip_nuisance(test_frame):
@@ -273,15 +273,16 @@ def test_fillna():
273273
r.fillna(0)
274274

275275

276-
def test_apply_without_aggregation():
277-
276+
@pytest.mark.parametrize(
277+
"func",
278+
[lambda x: x.resample("20min"), lambda x: x.groupby(pd.Grouper(freq="20min"))],
279+
ids=["resample", "groupby"],
280+
)
281+
def test_apply_without_aggregation(func):
278282
# both resample and groupby should work w/o aggregation
279-
r = test_series.resample("20min")
280-
g = test_series.groupby(pd.Grouper(freq="20min"))
281-
282-
for t in [g, r]:
283-
result = t.apply(lambda x: x)
284-
tm.assert_series_equal(result, test_series)
283+
t = func(test_series)
284+
result = t.apply(lambda x: x)
285+
tm.assert_series_equal(result, test_series)
285286

286287

287288
def test_agg_consistency():

pandas/tests/resample/test_resampler_grouper.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -202,44 +202,51 @@ def test_nearest():
202202
tm.assert_series_equal(result, expected)
203203

204204

205-
def test_methods():
205+
@pytest.mark.parametrize(
206+
"f",
207+
[
208+
"first",
209+
"last",
210+
"median",
211+
"sem",
212+
"sum",
213+
"mean",
214+
"min",
215+
"max",
216+
"size",
217+
"count",
218+
"nearest",
219+
"bfill",
220+
"ffill",
221+
"asfreq",
222+
"ohlc",
223+
],
224+
)
225+
def test_methods(f):
206226
g = test_frame.groupby("A")
207227
r = g.resample("2s")
208228

209-
for f in ["first", "last", "median", "sem", "sum", "mean", "min", "max"]:
210-
result = getattr(r, f)()
211-
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
212-
tm.assert_frame_equal(result, expected)
213-
214-
for f in ["size"]:
215-
result = getattr(r, f)()
216-
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
217-
tm.assert_series_equal(result, expected)
229+
result = getattr(r, f)()
230+
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
231+
tm.assert_equal(result, expected)
218232

219-
for f in ["count"]:
220-
result = getattr(r, f)()
221-
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
222-
tm.assert_frame_equal(result, expected)
223233

234+
def test_methods_nunique():
224235
# series only
225-
for f in ["nunique"]:
226-
result = getattr(r.B, f)()
227-
expected = g.B.apply(lambda x: getattr(x.resample("2s"), f)())
228-
tm.assert_series_equal(result, expected)
229-
230-
for f in ["nearest", "bfill", "ffill", "asfreq"]:
231-
result = getattr(r, f)()
232-
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
233-
tm.assert_frame_equal(result, expected)
234-
235-
result = r.ohlc()
236-
expected = g.apply(lambda x: x.resample("2s").ohlc())
237-
tm.assert_frame_equal(result, expected)
236+
g = test_frame.groupby("A")
237+
r = g.resample("2s")
238+
result = r.B.nunique()
239+
expected = g.B.apply(lambda x: x.resample("2s").nunique())
240+
tm.assert_series_equal(result, expected)
241+
238242

239-
for f in ["std", "var"]:
240-
result = getattr(r, f)(ddof=1)
241-
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
242-
tm.assert_frame_equal(result, expected)
243+
@pytest.mark.parametrize("f", ["std", "var"])
244+
def test_methods_std_var(f):
245+
g = test_frame.groupby("A")
246+
r = g.resample("2s")
247+
result = getattr(r, f)(ddof=1)
248+
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
249+
tm.assert_frame_equal(result, expected)
243250

244251

245252
def test_apply():

pandas/tests/resample/test_time_grouper.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,31 @@ def test_aggregate_normal(resample_method):
145145
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
146146
tm.assert_equal(expected, dt_result)
147147

148-
# if TimeGrouper is used included, 'nth' doesn't work yet
149-
150-
"""
151-
for func in ['nth']:
152-
expected = getattr(normal_grouped, func)(3)
153-
expected.index = date_range(start='2013-01-01',
154-
freq='D', periods=5, name='key')
155-
dt_result = getattr(dt_grouped, func)(3)
156-
tm.assert_frame_equal(expected, dt_result)
157-
"""
148+
149+
@pytest.mark.xfail(reason="if TimeGrouper is used included, 'nth' doesn't work yet")
150+
def test_aggregate_nth(resample_method):
151+
"""Check TimeGrouper's aggregation is identical as normal groupby."""
152+
153+
data = np.random.randn(20, 4)
154+
normal_df = DataFrame(data, columns=["A", "B", "C", "D"])
155+
normal_df["key"] = [1, 2, 3, 4, 5] * 4
156+
157+
dt_df = DataFrame(data, columns=["A", "B", "C", "D"])
158+
dt_df["key"] = [
159+
datetime(2013, 1, 1),
160+
datetime(2013, 1, 2),
161+
datetime(2013, 1, 3),
162+
datetime(2013, 1, 4),
163+
datetime(2013, 1, 5),
164+
] * 4
165+
166+
normal_grouped = normal_df.groupby("key")
167+
dt_grouped = dt_df.groupby(Grouper(key="key", freq="D"))
168+
169+
expected = normal_grouped.nth(3)
170+
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
171+
dt_result = dt_grouped.nth(3)
172+
tm.assert_frame_equal(expected, dt_result)
158173

159174

160175
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)