diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 3f6c06e20b546..9764f0febda60 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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`) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 99220232114ce..7e046c123df9d 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -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 diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index df4707fcef3f0..c142ca44f66cf 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -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',