Skip to content

Commit 819fc15

Browse files
ArtificialQualiaWillAyd
authored andcommitted
BUG: Fix groupby with MultiIndex Series corner case (#25704) (#25743)
1 parent f0ba498 commit 819fc15

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Groupby/Resample/Rolling
371371
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
372372
- Bug in :func:`pandas.core.groupby.GroupBy.agg` when applying a aggregation function to timezone aware data (:issue:`23683`)
373373
- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` where timezone information would be dropped (:issue:`21603`)
374+
- Bug in :func:`Series.groupby` where using ``groupby`` with a :class:`MultiIndex` Series with a list of labels equal to the length of the series caused incorrect grouping (:issue:`25704`)
374375
- Ensured that ordering of outputs in ``groupby`` aggregation functions is consistent across all versions of Python (:issue:`25692`)
375376
- Bug in :func:`idxmax` and :func:`idxmin` on :meth:`DataFrame.groupby` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
376377

pandas/core/groupby/grouper.py

+2
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
524524
if isinstance(obj, DataFrame):
525525
all_in_columns_index = all(g in obj.columns or g in obj.index.names
526526
for g in keys)
527+
elif isinstance(obj, Series):
528+
all_in_columns_index = all(g in obj.index.names for g in keys)
527529
else:
528530
all_in_columns_index = False
529531
except Exception:

pandas/tests/groupby/test_groupby.py

+20
Original file line numberDiff line numberDiff line change
@@ -1719,3 +1719,23 @@ def test_groupby_empty_list_raises():
17191719
msg = "Grouper and axis must be same length"
17201720
with pytest.raises(ValueError, match=msg):
17211721
df.groupby([[]])
1722+
1723+
1724+
def test_groupby_multiindex_series_keys_len_equal_group_axis():
1725+
# GH 25704
1726+
index_array = [
1727+
['x', 'x'],
1728+
['a', 'b'],
1729+
['k', 'k']
1730+
]
1731+
index_names = ['first', 'second', 'third']
1732+
ri = pd.MultiIndex.from_arrays(index_array, names=index_names)
1733+
s = pd.Series(data=[1, 2], index=ri)
1734+
result = s.groupby(['first', 'third']).sum()
1735+
1736+
index_array = [['x'], ['k']]
1737+
index_names = ['first', 'third']
1738+
ei = pd.MultiIndex.from_arrays(index_array, names=index_names)
1739+
expected = pd.Series([3], index=ei)
1740+
1741+
assert_series_equal(result, expected)

0 commit comments

Comments
 (0)