-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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