Skip to content

Commit

Permalink
TST/CLN: Remove groupby.test_allowlist (#52880)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach committed Apr 23, 2023
1 parent f38daad commit e28d17b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 124 deletions.
124 changes: 0 additions & 124 deletions pandas/tests/groupby/test_allowlist.py

This file was deleted.

50 changes: 50 additions & 0 deletions pandas/tests/groupby/test_function.py
Expand Up @@ -1662,3 +1662,53 @@ def test_duplicate_columns(request, groupby_func, as_index):
if groupby_func not in ("size", "ngroup", "cumcount"):
expected = expected.rename(columns={"c": "b"})
tm.assert_equal(result, expected)


@pytest.mark.parametrize(
"op",
[
"sum",
"prod",
"min",
"max",
"median",
"mean",
"skew",
"std",
"var",
"sem",
],
)
@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize("skipna", [True, False])
@pytest.mark.parametrize("sort", [True, False])
def test_regression_allowlist_methods(op, axis, skipna, sort):
# GH6944
# GH 17537
# explicitly test the allowlist methods
raw_frame = DataFrame([0])
if axis == 0:
frame = raw_frame
msg = "The 'axis' keyword in DataFrame.groupby is deprecated and will be"
else:
frame = raw_frame.T
msg = "DataFrame.groupby with axis=1 is deprecated"

with tm.assert_produces_warning(FutureWarning, match=msg):
grouped = frame.groupby(level=0, axis=axis, sort=sort)

if op == "skew":
# skew has skipna
result = getattr(grouped, op)(skipna=skipna)
expected = frame.groupby(level=0).apply(
lambda h: getattr(h, op)(axis=axis, skipna=skipna)
)
if sort:
expected = expected.sort_index(axis=axis)
tm.assert_frame_equal(result, expected)
else:
result = getattr(grouped, op)()
expected = frame.groupby(level=0).apply(lambda h: getattr(h, op)(axis=axis))
if sort:
expected = expected.sort_index(axis=axis)
tm.assert_frame_equal(result, expected)
62 changes: 62 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Expand Up @@ -2981,3 +2981,65 @@ def test_groupby_sum_on_nan_should_return_nan(bug_var):

expected_df = DataFrame([bug_var, bug_var, bug_var, np.nan], columns=["A"])
tm.assert_frame_equal(result, expected_df)


@pytest.mark.parametrize(
"method",
[
"count",
"corr",
"cummax",
"cummin",
"cumprod",
"describe",
"rank",
"quantile",
"diff",
"shift",
"all",
"any",
"idxmin",
"idxmax",
"ffill",
"bfill",
"pct_change",
],
)
def test_groupby_selection_with_methods(df, method):
# some methods which require DatetimeIndex
rng = date_range("2014", periods=len(df))
df.index = rng

g = df.groupby(["A"])[["C"]]
g_exp = df[["C"]].groupby(df["A"])
# TODO check groupby with > 1 col ?

res = getattr(g, method)()
exp = getattr(g_exp, method)()

# should always be frames!
tm.assert_frame_equal(res, exp)


def test_groupby_selection_other_methods(df):
# some methods which require DatetimeIndex
rng = date_range("2014", periods=len(df))
df.columns.name = "foo"
df.index = rng

g = df.groupby(["A"])[["C"]]
g_exp = df[["C"]].groupby(df["A"])

# methods which aren't just .foo()
tm.assert_frame_equal(g.fillna(0), g_exp.fillna(0))
msg = "DataFrameGroupBy.dtypes is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
tm.assert_frame_equal(g.apply(lambda x: x.sum()), g_exp.apply(lambda x: x.sum()))

tm.assert_frame_equal(g.resample("D").mean(), g_exp.resample("D").mean())
tm.assert_frame_equal(g.resample("D").ohlc(), g_exp.resample("D").ohlc())

tm.assert_frame_equal(
g.filter(lambda x: len(x) == 3), g_exp.filter(lambda x: len(x) == 3)
)

0 comments on commit e28d17b

Please sign in to comment.