From cedb6ee009dfda5f437ae6cd11b62db9b15d1c18 Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Fri, 13 Nov 2020 22:31:46 -0500 Subject: [PATCH] Backport PR #37801 on branch 1.1.x: REGR: SeriesGroupBy where index has a tuple name fails --- doc/source/whatsnew/v1.1.5.rst | 1 + pandas/core/series.py | 6 +++--- pandas/tests/groupby/test_groupby.py | 10 ++++++++++ pandas/tests/series/indexing/test_indexing.py | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.1.5.rst b/doc/source/whatsnew/v1.1.5.rst index 3b1f64e730830..2a598c489e809 100644 --- a/doc/source/whatsnew/v1.1.5.rst +++ b/doc/source/whatsnew/v1.1.5.rst @@ -15,6 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Regression in addition of a timedelta-like scalar to a :class:`DatetimeIndex` raising incorrectly (:issue:`37295`) +- Fixed regression in :meth:`Series.groupby` raising when the :class:`Index` of the :class:`Series` had a tuple as its name (:issue:`37755`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index 18a201674db65..277aa1d095350 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -889,7 +889,7 @@ def __getitem__(self, key): return result - except KeyError: + except (KeyError, TypeError): if isinstance(key, tuple) and isinstance(self.index, MultiIndex): # We still have the corner case where a tuple is a key # in the first level of our MultiIndex @@ -953,7 +953,7 @@ def _get_values_tuple(self, key): return result if not isinstance(self.index, MultiIndex): - raise ValueError("key of type tuple not found and not a MultiIndex") + raise KeyError("key of type tuple not found and not a MultiIndex") # If key is contained, would have returned by now indexer, new_index = self.index.get_loc_level(key) @@ -1009,7 +1009,7 @@ def __setitem__(self, key, value): except TypeError as err: if isinstance(key, tuple) and not isinstance(self.index, MultiIndex): - raise ValueError( + raise KeyError( "key of type tuple not found and not a MultiIndex" ) from err diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index bdb283ae445b1..35eab708412b8 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2055,3 +2055,13 @@ def test_groups_repr_truncates(max_seq_items, expected): result = df.groupby(np.array(df.a)).groups.__repr__() assert result == expected + + +def test_groupby_series_with_tuple_name(): + # GH 37755 + ser = Series([1, 2, 3, 4], index=[1, 1, 2, 2], name=("a", "a")) + ser.index.name = ("b", "b") + result = ser.groupby(level=0).last() + expected = Series([2, 4], index=[1, 2], name=("a", "a")) + expected.index.name = ("b", "b") + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index fbdac2bb2d8e8..3b66939d9ddd2 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -387,9 +387,9 @@ def test_2d_to_1d_assignment_raises(): def test_basic_getitem_setitem_corner(datetime_series): # invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2] msg = "key of type tuple not found and not a MultiIndex" - with pytest.raises(ValueError, match=msg): + with pytest.raises(KeyError, match=msg): datetime_series[:, 2] - with pytest.raises(ValueError, match=msg): + with pytest.raises(KeyError, match=msg): datetime_series[:, 2] = 2 # weird lists. [slice(0, 5)] will work but not two slices