Skip to content

TST: Move groupby tests out of test_function #55967

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
Nov 16, 2023
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
6 changes: 6 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ def test_agg_multiple_functions_maintain_order(df):
tm.assert_index_equal(result.columns, exp_cols)


def test_series_index_name(df):
grouped = df.loc[:, ["C"]].groupby(df["A"])
result = grouped.agg(lambda x: x.mean())
assert result.index.name == "A"


def test_agg_multiple_functions_same_name():
# GH 30880
df = DataFrame(
Expand Down
70 changes: 70 additions & 0 deletions pandas/tests/groupby/methods/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,73 @@ def test_describe_duplicate_columns():
)
expected.index.names = [1]
tm.assert_frame_equal(result, expected)


class TestGroupByNonCythonPaths:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for a follow up, would be good to collapse this class into just the testing function and fixtures

# GH#5610 non-cython calls should not include the grouper
# Tests for code not expected to go through cython paths.

@pytest.fixture
def df(self):
df = DataFrame(
[[1, 2, "foo"], [1, np.nan, "bar"], [3, np.nan, "baz"]],
columns=["A", "B", "C"],
)
return df

@pytest.fixture
def gb(self, df):
gb = df.groupby("A")
return gb

@pytest.fixture
def gni(self, df):
gni = df.groupby("A", as_index=False)
return gni

def test_describe(self, df, gb, gni):
# describe
expected_index = Index([1, 3], name="A")
expected_col = MultiIndex(
levels=[["B"], ["count", "mean", "std", "min", "25%", "50%", "75%", "max"]],
codes=[[0] * 8, list(range(8))],
)
expected = DataFrame(
[
[1.0, 2.0, np.nan, 2.0, 2.0, 2.0, 2.0, 2.0],
[0.0, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
],
index=expected_index,
columns=expected_col,
)
result = gb.describe()
tm.assert_frame_equal(result, expected)

expected = expected.reset_index()
result = gni.describe()
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("dtype", [int, float, object])
@pytest.mark.parametrize(
"kwargs",
[
{"percentiles": [0.10, 0.20, 0.30], "include": "all", "exclude": None},
{"percentiles": [0.10, 0.20, 0.30], "include": None, "exclude": ["int"]},
{"percentiles": [0.10, 0.20, 0.30], "include": ["int"], "exclude": None},
],
)
def test_groupby_empty_dataset(dtype, kwargs):
# GH#41575
df = DataFrame([[1, 2, 3]], columns=["A", "B", "C"], dtype=dtype)
df["B"] = df["B"].astype(int)
df["C"] = df["C"].astype(float)

result = df.iloc[:0].groupby("A").describe(**kwargs)
expected = df.groupby("A").describe(**kwargs).reset_index(drop=True).iloc[:0]
tm.assert_frame_equal(result, expected)

result = df.iloc[:0].groupby("A").B.describe(**kwargs)
expected = df.groupby("A").B.describe(**kwargs).reset_index(drop=True).iloc[:0]
expected.index = Index([])
tm.assert_frame_equal(result, expected)
42 changes: 42 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,3 +1559,45 @@ def test_include_groups(include_groups):
if not include_groups:
expected = expected[["b"]]
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("f", [max, min, sum])
@pytest.mark.parametrize("keys", ["jim", ["jim", "joe"]]) # Single key # Multi-key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment might be clearer if instead of being a comment it was encoded in ids=["single_key", "multi_key"]

def test_builtins_apply(keys, f):
# see gh-8155
rs = np.random.default_rng(2)
df = DataFrame(rs.integers(1, 7, (10, 2)), columns=["jim", "joe"])
df["jolie"] = rs.standard_normal(10)

gb = df.groupby(keys)

fname = f.__name__

warn = None if f is not sum else FutureWarning
msg = "The behavior of DataFrame.sum with axis=None is deprecated"
with tm.assert_produces_warning(
warn, match=msg, check_stacklevel=False, raise_on_extra_warnings=False
):
# Also warns on deprecation GH#53425
result = gb.apply(f)
ngroups = len(df.drop_duplicates(subset=keys))

assert_msg = f"invalid frame shape: {result.shape} (expected ({ngroups}, 3))"
assert result.shape == (ngroups, 3), assert_msg

npfunc = lambda x: getattr(np, fname)(x, axis=0) # numpy's equivalent function
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = gb.apply(npfunc)
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, match=msg):
expected2 = gb.apply(lambda x: npfunc(x))
tm.assert_frame_equal(result, expected2)

if f != sum:
expected = gb.agg(fname).reset_index()
expected.set_index(keys, inplace=True, drop=False)
tm.assert_frame_equal(result, expected, check_dtype=False)

tm.assert_series_equal(getattr(result, fname)(axis=0), getattr(df, fname)(axis=0))
27 changes: 27 additions & 0 deletions pandas/tests/groupby/test_cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,30 @@ def test_nullable_int_not_cast_as_float(method, dtype, val):
expected = DataFrame({"b": data}, dtype=dtype)

tm.assert_frame_equal(result, expected)


def test_cython_api2():
# this takes the fast apply path

# cumsum (GH5614)
df = DataFrame([[1, 2, np.nan], [1, np.nan, 9], [3, 4, 9]], columns=["A", "B", "C"])
expected = DataFrame([[2, np.nan], [np.nan, 9], [4, 9]], columns=["B", "C"])
result = df.groupby("A").cumsum()
tm.assert_frame_equal(result, expected)

# GH 5755 - cumsum is a transformer and should ignore as_index
result = df.groupby("A", as_index=False).cumsum()
tm.assert_frame_equal(result, expected)

# GH 13994
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NBD (in part bc it will be removed in a few months) but probably should be split out into a separate test

msg = "DataFrameGroupBy.cumsum with axis=1 is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.groupby("A").cumsum(axis=1)
expected = df.cumsum(axis=1)
tm.assert_frame_equal(result, expected)

msg = "DataFrameGroupBy.cumprod with axis=1 is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.groupby("A").cumprod(axis=1)
expected = df.cumprod(axis=1)
tm.assert_frame_equal(result, expected)
Loading