Skip to content

Commit 2000334

Browse files
Backport PR #36147: REGR: Series access with Index of tuples/frozenset (#36332)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent ffc0092 commit 2000334

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

doc/source/whatsnew/v1.1.3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
18+
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
1719
-
1820

1921
.. ---------------------------------------------------------------------------

pandas/core/series.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -881,21 +881,19 @@ def __getitem__(self, key):
881881
elif key_is_scalar:
882882
return self._get_value(key)
883883

884-
if (
885-
isinstance(key, tuple)
886-
and is_hashable(key)
887-
and isinstance(self.index, MultiIndex)
888-
):
884+
if is_hashable(key):
889885
# Otherwise index.get_value will raise InvalidIndexError
890886
try:
887+
# For labels that don't resolve as scalars like tuples and frozensets
891888
result = self._get_value(key)
892889

893890
return result
894891

895892
except KeyError:
896-
# We still have the corner case where this tuple is a key
897-
# in the first level of our MultiIndex
898-
return self._get_values_tuple(key)
893+
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
894+
# We still have the corner case where a tuple is a key
895+
# in the first level of our MultiIndex
896+
return self._get_values_tuple(key)
899897

900898
if is_iterator(key):
901899
key = list(key)
@@ -955,7 +953,7 @@ def _get_values_tuple(self, key):
955953
return result
956954

957955
if not isinstance(self.index, MultiIndex):
958-
raise ValueError("Can only tuple-index with a MultiIndex")
956+
raise ValueError("key of type tuple not found and not a MultiIndex")
959957

960958
# If key is contained, would have returned by now
961959
indexer, new_index = self.index.get_loc_level(key)
@@ -1009,9 +1007,11 @@ def __setitem__(self, key, value):
10091007
# GH#12862 adding an new key to the Series
10101008
self.loc[key] = value
10111009

1012-
except TypeError as e:
1010+
except TypeError as err:
10131011
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
1014-
raise ValueError("Can only tuple-index with a MultiIndex") from e
1012+
raise ValueError(
1013+
"key of type tuple not found and not a MultiIndex"
1014+
) from err
10151015

10161016
if com.is_bool_indexer(key):
10171017
key = check_bool_indexer(self.index, key)

pandas/tests/series/indexing/test_indexing.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def test_2d_to_1d_assignment_raises():
383383
@pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning")
384384
def test_basic_getitem_setitem_corner(datetime_series):
385385
# invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2]
386-
msg = "Can only tuple-index with a MultiIndex"
386+
msg = "key of type tuple not found and not a MultiIndex"
387387
with pytest.raises(ValueError, match=msg):
388388
datetime_series[:, 2]
389389
with pytest.raises(ValueError, match=msg):
@@ -942,3 +942,22 @@ def assert_slices_equivalent(l_slc, i_slc):
942942
for key2 in [keystr2, box(keystr2)]:
943943
assert_slices_equivalent(SLC[key2:key:-1], SLC[13:8:-1])
944944
assert_slices_equivalent(SLC[key:key2:-1], SLC[0:0:-1])
945+
946+
947+
def test_tuple_index():
948+
# GH 35534 - Selecting values when a Series has an Index of tuples
949+
s = pd.Series([1, 2], index=[("a",), ("b",)])
950+
assert s[("a",)] == 1
951+
assert s[("b",)] == 2
952+
s[("b",)] = 3
953+
assert s[("b",)] == 3
954+
955+
956+
def test_frozenset_index():
957+
# GH35747 - Selecting values when a Series has an Index of frozenset
958+
idx0, idx1 = frozenset("a"), frozenset("b")
959+
s = pd.Series([1, 2], index=[idx0, idx1])
960+
assert s[idx0] == 1
961+
assert s[idx1] == 2
962+
s[idx1] = 3
963+
assert s[idx1] == 3

0 commit comments

Comments
 (0)