Skip to content

Commit be03eb1

Browse files
authored
REGR: SeriesGroupBy where index has a tuple name fails (pandas-dev#37801)
1 parent 9bb6721 commit be03eb1

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Regression in addition of a timedelta-like scalar to a :class:`DatetimeIndex` raising incorrectly (:issue:`37295`)
18+
- Fixed regression in :meth:`Series.groupby` raising when the :class:`Index` of the :class:`Series` had a tuple as its name (:issue:`37755`)
1819
-
1920

2021
.. ---------------------------------------------------------------------------

pandas/core/series.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ def __getitem__(self, key):
900900

901901
return result
902902

903-
except KeyError:
903+
except (KeyError, TypeError):
904904
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
905905
# We still have the corner case where a tuple is a key
906906
# in the first level of our MultiIndex
@@ -964,7 +964,7 @@ def _get_values_tuple(self, key):
964964
return result
965965

966966
if not isinstance(self.index, MultiIndex):
967-
raise ValueError("key of type tuple not found and not a MultiIndex")
967+
raise KeyError("key of type tuple not found and not a MultiIndex")
968968

969969
# If key is contained, would have returned by now
970970
indexer, new_index = self.index.get_loc_level(key)
@@ -1020,7 +1020,7 @@ def __setitem__(self, key, value):
10201020

10211021
except TypeError as err:
10221022
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
1023-
raise ValueError(
1023+
raise KeyError(
10241024
"key of type tuple not found and not a MultiIndex"
10251025
) from err
10261026

pandas/tests/groupby/test_groupby.py

+10
Original file line numberDiff line numberDiff line change
@@ -2146,3 +2146,13 @@ def test_groupby_duplicate_columns():
21462146
result = df.groupby([0, 0, 0, 0]).min()
21472147
expected = DataFrame([["e", "a", 1]], columns=["A", "B", "B"])
21482148
tm.assert_frame_equal(result, expected)
2149+
2150+
2151+
def test_groupby_series_with_tuple_name():
2152+
# GH 37755
2153+
ser = Series([1, 2, 3, 4], index=[1, 1, 2, 2], name=("a", "a"))
2154+
ser.index.name = ("b", "b")
2155+
result = ser.groupby(level=0).last()
2156+
expected = Series([2, 4], index=[1, 2], name=("a", "a"))
2157+
expected.index.name = ("b", "b")
2158+
tm.assert_series_equal(result, expected)

pandas/tests/series/indexing/test_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ def test_loc_setitem_2d_to_1d_raises():
327327
def test_basic_getitem_setitem_corner(datetime_series):
328328
# invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2]
329329
msg = "key of type tuple not found and not a MultiIndex"
330-
with pytest.raises(ValueError, match=msg):
330+
with pytest.raises(KeyError, match=msg):
331331
datetime_series[:, 2]
332-
with pytest.raises(ValueError, match=msg):
332+
with pytest.raises(KeyError, match=msg):
333333
datetime_series[:, 2] = 2
334334

335335
# weird lists. [slice(0, 5)] will work but not two slices

0 commit comments

Comments
 (0)