Skip to content
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

BUG: .groupby on Series' values without reindexing #15340

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ Bug Fixes

- Bug in groupby operations with timedelta64 when passing ``numeric_only=False`` (:issue:`5724`)

- Bug in ``.groupby()`` when grouping on a ``Series`` with same-length but incompatible index (:issue:`15338`)

- Bug in ``DataFrame.to_html`` with ``index=False`` and ``max_rows`` raising in ``IndexError`` (:issue:`14998`)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,7 @@ def _convert_grouper(axis, grouper):
if isinstance(grouper, dict):
return grouper.get
elif isinstance(grouper, Series):
if grouper.index.equals(axis):
if len(grouper.index) == len(axis):
return grouper._values
else:
return grouper.reindex(axis)._values
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,17 @@ def test_groupby_series_indexed_differently(self):
exp = s1.groupby(s2.reindex(s1.index).get).mean()
assert_series_equal(agged, exp)

def test_groupby_series_values(self):
# GH-15338
s = pd.Series([1, 2, 3], index=range(3))
df = pd.DataFrame(np.arange(6).reshape(3, 2), index=range(3))
key = pd.Series([0, 0, 1], index=range(3, 6)) # Disjunct index

assert_series_equal(s.groupby(key).sum(),
s.groupby(key.values).sum())
assert_frame_equal(df.groupby(key).sum(),
df.groupby(key.values).sum())

def test_groupby_with_hier_columns(self):
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux',
'qux'], ['one', 'two', 'one', 'two', 'one', 'two',
Expand Down