Skip to content

Commit 7468864

Browse files
Backport PR pandas-dev#37801 on branch 1.1.x: REGR: SeriesGroupBy where index has a tuple name fails (pandas-dev#37829)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent 8f7d1d5 commit 7468864

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
@@ -889,7 +889,7 @@ def __getitem__(self, key):
889889

890890
return result
891891

892-
except KeyError:
892+
except (KeyError, TypeError):
893893
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
894894
# We still have the corner case where a tuple is a key
895895
# in the first level of our MultiIndex
@@ -953,7 +953,7 @@ def _get_values_tuple(self, key):
953953
return result
954954

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

958958
# If key is contained, would have returned by now
959959
indexer, new_index = self.index.get_loc_level(key)
@@ -1009,7 +1009,7 @@ def __setitem__(self, key, value):
10091009

10101010
except TypeError as err:
10111011
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
1012-
raise ValueError(
1012+
raise KeyError(
10131013
"key of type tuple not found and not a MultiIndex"
10141014
) from err
10151015

pandas/tests/groupby/test_groupby.py

+10
Original file line numberDiff line numberDiff line change
@@ -2055,3 +2055,13 @@ def test_groups_repr_truncates(max_seq_items, expected):
20552055

20562056
result = df.groupby(np.array(df.a)).groups.__repr__()
20572057
assert result == expected
2058+
2059+
2060+
def test_groupby_series_with_tuple_name():
2061+
# GH 37755
2062+
ser = Series([1, 2, 3, 4], index=[1, 1, 2, 2], name=("a", "a"))
2063+
ser.index.name = ("b", "b")
2064+
result = ser.groupby(level=0).last()
2065+
expected = Series([2, 4], index=[1, 2], name=("a", "a"))
2066+
expected.index.name = ("b", "b")
2067+
tm.assert_series_equal(result, expected)

pandas/tests/series/indexing/test_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ def test_2d_to_1d_assignment_raises():
387387
def test_basic_getitem_setitem_corner(datetime_series):
388388
# invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2]
389389
msg = "key of type tuple not found and not a MultiIndex"
390-
with pytest.raises(ValueError, match=msg):
390+
with pytest.raises(KeyError, match=msg):
391391
datetime_series[:, 2]
392-
with pytest.raises(ValueError, match=msg):
392+
with pytest.raises(KeyError, match=msg):
393393
datetime_series[:, 2] = 2
394394

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

0 commit comments

Comments
 (0)