Skip to content

TST: Split / parameterize resample PT 2 #45282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ def test_attribute_access(test_frame):
tm.assert_series_equal(r.A.sum(), r["A"].sum())


def test_api_compat_before_use():
@pytest.mark.parametrize("attr", ["groups", "ngroups", "indices"])
def test_api_compat_before_use(attr):

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

# before use
getattr(rs, attr)
# before use
getattr(rs, attr)

# after grouper is initialized is ok
rs.mean()
getattr(rs, attr)
# after grouper is initialized is ok
rs.mean()
getattr(rs, attr)


def tests_skip_nuisance(test_frame):
Expand Down Expand Up @@ -273,15 +273,16 @@ def test_fillna():
r.fillna(0)


def test_apply_without_aggregation():

@pytest.mark.parametrize(
"func",
[lambda x: x.resample("20min"), lambda x: x.groupby(pd.Grouper(freq="20min"))],
ids=["resample", "groupby"],
)
def test_apply_without_aggregation(func):
# both resample and groupby should work w/o aggregation
r = test_series.resample("20min")
g = test_series.groupby(pd.Grouper(freq="20min"))

for t in [g, r]:
result = t.apply(lambda x: x)
tm.assert_series_equal(result, test_series)
t = func(test_series)
result = t.apply(lambda x: x)
tm.assert_series_equal(result, test_series)


def test_agg_consistency():
Expand Down
69 changes: 38 additions & 31 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,44 +202,51 @@ def test_nearest():
tm.assert_series_equal(result, expected)


def test_methods():
@pytest.mark.parametrize(
"f",
[
"first",
"last",
"median",
"sem",
"sum",
"mean",
"min",
"max",
"size",
"count",
"nearest",
"bfill",
"ffill",
"asfreq",
"ohlc",
],
)
def test_methods(f):
g = test_frame.groupby("A")
r = g.resample("2s")

for f in ["first", "last", "median", "sem", "sum", "mean", "min", "max"]:
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
tm.assert_frame_equal(result, expected)

for f in ["size"]:
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
tm.assert_series_equal(result, expected)
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
tm.assert_equal(result, expected)

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

def test_methods_nunique():
# series only
for f in ["nunique"]:
result = getattr(r.B, f)()
expected = g.B.apply(lambda x: getattr(x.resample("2s"), f)())
tm.assert_series_equal(result, expected)

for f in ["nearest", "bfill", "ffill", "asfreq"]:
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.resample("2s"), f)())
tm.assert_frame_equal(result, expected)

result = r.ohlc()
expected = g.apply(lambda x: x.resample("2s").ohlc())
tm.assert_frame_equal(result, expected)
g = test_frame.groupby("A")
r = g.resample("2s")
result = r.B.nunique()
expected = g.B.apply(lambda x: x.resample("2s").nunique())
tm.assert_series_equal(result, expected)


for f in ["std", "var"]:
result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("f", ["std", "var"])
def test_methods_std_var(f):
g = test_frame.groupby("A")
r = g.resample("2s")
result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
tm.assert_frame_equal(result, expected)


def test_apply():
Expand Down
35 changes: 25 additions & 10 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,31 @@ def test_aggregate_normal(resample_method):
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
tm.assert_equal(expected, dt_result)

# if TimeGrouper is used included, 'nth' doesn't work yet

"""
for func in ['nth']:
expected = getattr(normal_grouped, func)(3)
expected.index = date_range(start='2013-01-01',
freq='D', periods=5, name='key')
dt_result = getattr(dt_grouped, func)(3)
tm.assert_frame_equal(expected, dt_result)
"""

@pytest.mark.xfail(reason="if TimeGrouper is used included, 'nth' doesn't work yet")
def test_aggregate_nth(resample_method):
"""Check TimeGrouper's aggregation is identical as normal groupby."""

data = np.random.randn(20, 4)
normal_df = DataFrame(data, columns=["A", "B", "C", "D"])
normal_df["key"] = [1, 2, 3, 4, 5] * 4

dt_df = DataFrame(data, columns=["A", "B", "C", "D"])
dt_df["key"] = [
datetime(2013, 1, 1),
datetime(2013, 1, 2),
datetime(2013, 1, 3),
datetime(2013, 1, 4),
datetime(2013, 1, 5),
] * 4

normal_grouped = normal_df.groupby("key")
dt_grouped = dt_df.groupby(Grouper(key="key", freq="D"))

expected = normal_grouped.nth(3)
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
dt_result = dt_grouped.nth(3)
tm.assert_frame_equal(expected, dt_result)


@pytest.mark.parametrize(
Expand Down