-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: do not crash on a callable grouper returning tuples #22279
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 |
---|---|---|
|
@@ -508,6 +508,15 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True, | |
warnings.warn(msg, FutureWarning, stacklevel=5) | ||
key = list(key) | ||
|
||
if callable(key) or isinstance(key, dict): | ||
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. Is the 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. (done - expanded the test) 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 integrate this with the below (e.g. i/elif maybe enough)? |
||
if level is None: | ||
key = group_axis.map(key) | ||
else: | ||
key = group_axis.get_level_values(level=level).map(key) | ||
# If the grouper is a mapping, 'level' is _only_ used to determine | ||
# the mapping input | ||
level = None | ||
|
||
if not isinstance(key, list): | ||
keys = [key] | ||
match_axis_length = False | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,23 @@ def test_grouper_creation_bug(self): | |
expected = s.groupby(level='one').sum() | ||
assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('func', [False, True]) | ||
def test_grouper_returning_tuples(self, func): | ||
# GH 22257 , both with dict and with callable | ||
df = pd.DataFrame({'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]}) | ||
mapping = dict(zip(range(4), [('C', 5), ('D', 6)] * 2)) | ||
|
||
if func: | ||
gb = df.groupby(by=lambda idx: mapping[idx], sort=False) | ||
else: | ||
gb = df.groupby(by=mapping, sort=False) | ||
|
||
name, expected = list(gb)[0] | ||
assert name == ('C', 5) | ||
result = gb.get_group(name) | ||
|
||
assert_frame_equal(result, expected) | ||
|
||
def test_grouper_column_and_index(self): | ||
# GH 14327 | ||
|
||
|
@@ -346,7 +363,7 @@ def test_groupby_grouper_f_sanity_checked(self): | |
# when the elements are Timestamp. | ||
# the result is Index[0:6], very confusing. | ||
|
||
pytest.raises(AssertionError, ts.groupby, lambda key: key[0:6]) | ||
pytest.raises(ValueError, ts.groupby, lambda key: key[0:6]) | ||
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. What is the reason for this change? 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 single callable/mapping case used to follow the same code path as lists of callables/mappings, while it now follows the same code path as e.g. lists of 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. Notice this test will also change when we remove the absurd |
||
|
||
def test_grouping_error_on_multidim_input(self, df): | ||
pytest.raises(ValueError, | ||
|
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.
let' move to 0.24