From 23c002beff489eec0eda45c86ee1b56eae9131aa Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Thu, 12 Nov 2020 18:37:10 -0500 Subject: [PATCH 1/3] REGR: SeriesGroupBy where index has a tuple name fails --- pandas/core/series.py | 6 +++--- pandas/tests/groupby/test_groupby.py | 10 ++++++++++ pandas/tests/series/indexing/test_indexing.py | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index f243771ff97a5..800da18142825 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -900,7 +900,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 @@ -964,7 +964,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) @@ -1020,7 +1020,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 3a4abd58f0d39..d01a6fde0e2c5 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2146,3 +2146,13 @@ def test_groupby_duplicate_columns(): result = df.groupby([0, 0, 0, 0]).min() expected = DataFrame([["e", "a", 1]], columns=["A", "B", "B"]) tm.assert_frame_equal(result, expected) + + +def test_groupby_series_with_tuple_name(): + # GH 37755 + s = Series([1, 2, 3, 4], index=[1, 1, 2, 2], name=("a", "a")) + s.index.name = ("b", "b") + result = s.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 1f2adaafbbccd..682c057f05700 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -327,9 +327,9 @@ def test_loc_setitem_2d_to_1d_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 From 75e14850e3a6490964f584b1893b9599db348f5f Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Thu, 12 Nov 2020 18:41:41 -0500 Subject: [PATCH 2/3] whatsnew --- doc/source/whatsnew/v1.1.5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.5.rst b/doc/source/whatsnew/v1.1.5.rst index a29ae1912e338..364cf8278436f 100644 --- a/doc/source/whatsnew/v1.1.5.rst +++ b/doc/source/whatsnew/v1.1.5.rst @@ -14,7 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Fixed regression in :meth:`Series.groupby` raising when the :class:`Index` of the :class:`Series` had a tuple as its name (:issue:`37755`) - .. --------------------------------------------------------------------------- From 2dc5744da803b53ea7933616792185a04531bc1f Mon Sep 17 00:00:00 2001 From: rhshadrach Date: Fri, 13 Nov 2020 20:47:34 -0500 Subject: [PATCH 3/3] s -> ser in test --- pandas/tests/groupby/test_groupby.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index d01a6fde0e2c5..cd1fc67772849 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2150,9 +2150,9 @@ def test_groupby_duplicate_columns(): def test_groupby_series_with_tuple_name(): # GH 37755 - s = Series([1, 2, 3, 4], index=[1, 1, 2, 2], name=("a", "a")) - s.index.name = ("b", "b") - result = s.groupby(level=0).last() + 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)