-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Groupby transform cleanups #27467
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
Groupby transform cleanups #27467
Changes from 7 commits
071c9fa
0118cc8
2f29e4e
45e9f70
0a1a0fb
b845034
f45acd5
514528b
d662c9b
3c56a62
52d4a61
a6a681a
8b572ec
4e3ba63
0ad156b
b1cb4b0
d445de4
de2c59c
bd73cff
f979ef8
7d7eecc
d343686
6d0301a
b6e924e
399804c
1d158de
04dadd9
146e402
932d224
897d2fb
6a80828
2555193
3d7bc64
2b61329
053320b
994ab5f
6b90418
c932afd
5f10720
4b186a4
de0c11c
cba02d6
b19a46f
3511480
b5d3931
c97e2f2
e9343e1
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
concat, | ||
date_range, | ||
) | ||
from pandas.core.groupby.base import reduction_functions | ||
from pandas.core.groupby.groupby import DataError | ||
from pandas.util import testing as tm | ||
from pandas.util.testing import assert_frame_equal, assert_series_equal | ||
|
@@ -1001,3 +1002,47 @@ def test_ffill_not_in_axis(func, key, val): | |
expected = df | ||
|
||
assert_frame_equal(result, expected) | ||
|
||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_transform_invalid_name_raises(): | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
df = DataFrame(dict(a=[0, 1, 1, 2])) | ||
g = df.groupby(["a", "b", "b", "c"]) | ||
with pytest.raises(ValueError, match="not a valid function name"): | ||
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. Can you parametrize these instead? 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. I don't think that's needed. Parameterize with one case is not useful, and as a smoke test there doesn't need to be more than one. Also lots of similar tests in this file. |
||
g.transform("some_arbitrary_name") | ||
|
||
# method exists on the object, but is not a valid transformation/agg | ||
assert hasattr(g, "aggregate") # make sure the method exists | ||
with pytest.raises(ValueError, match="not a valid function name"): | ||
g.transform("aggregate") | ||
|
||
# Test SeriesGroupBy | ||
g = df["a"].groupby(["a", "b", "b", "c"]) | ||
with pytest.raises(ValueError, match="not a valid function name"): | ||
g.transform("some_arbitrary_name") | ||
|
||
# method exists on the object, but is not a valid transformation/agg | ||
with pytest.raises(ValueError, match="not a valid function name"): | ||
g.transform("aggregate") | ||
|
||
|
||
@pytest.mark.parametrize("func", sorted(reduction_functions)) | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
def test_transform_agg_by_name(func): | ||
|
||
df = DataFrame(dict(a=[0, 0, 0, 1, 1, 1], b=range(6))) | ||
g = df.groupby(np.repeat([0, 1], 3)) | ||
|
||
if func == "ngroup": # GH#27468 | ||
pytest.xfail("TODO: g.transform('ngroup') doesn't work") | ||
if func == "size": # GH#27469 | ||
pytest.xfail("TODO: g.transform('size') doesn't work") | ||
if func == "corr": | ||
pytest.xfail("corr returns multiindex, excluded from transform for now") | ||
|
||
args = {"nth": [0], "quantile": [0.5]}.get(func, []) | ||
|
||
print(func) | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
result = g.transform(func, *args) | ||
tm.assert_index_equal(result.index, df.index) | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
||
# check values replicated broadcasted across group | ||
assert len(set(result.iloc[-3:, 1])) == 1 |
Uh oh!
There was an error while loading. Please reload this page.