Skip to content

Commit bcbac92

Browse files
rhshadrachKevin D Smith
authored and
Kevin D Smith
committed
REGR: Series access with Index of tuples/frozenset (pandas-dev#36147)
1 parent c8392b7 commit bcbac92

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
@@ -889,21 +889,19 @@ def __getitem__(self, key):
889889
elif key_is_scalar:
890890
return self._get_value(key)
891891

892-
if (
893-
isinstance(key, tuple)
894-
and is_hashable(key)
895-
and isinstance(self.index, MultiIndex)
896-
):
892+
if is_hashable(key):
897893
# Otherwise index.get_value will raise InvalidIndexError
898894
try:
895+
# For labels that don't resolve as scalars like tuples and frozensets
899896
result = self._get_value(key)
900897

901898
return result
902899

903900
except KeyError:
904-
# We still have the corner case where this tuple is a key
905-
# in the first level of our MultiIndex
906-
return self._get_values_tuple(key)
901+
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
902+
# We still have the corner case where a tuple is a key
903+
# in the first level of our MultiIndex
904+
return self._get_values_tuple(key)
907905

908906
if is_iterator(key):
909907
key = list(key)
@@ -963,7 +961,7 @@ def _get_values_tuple(self, key):
963961
return result
964962

965963
if not isinstance(self.index, MultiIndex):
966-
raise ValueError("Can only tuple-index with a MultiIndex")
964+
raise ValueError("key of type tuple not found and not a MultiIndex")
967965

968966
# If key is contained, would have returned by now
969967
indexer, new_index = self.index.get_loc_level(key)
@@ -1017,9 +1015,11 @@ def __setitem__(self, key, value):
10171015
# GH#12862 adding an new key to the Series
10181016
self.loc[key] = value
10191017

1020-
except TypeError as e:
1018+
except TypeError as err:
10211019
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
1022-
raise ValueError("Can only tuple-index with a MultiIndex") from e
1020+
raise ValueError(
1021+
"key of type tuple not found and not a MultiIndex"
1022+
) from err
10231023

10241024
if com.is_bool_indexer(key):
10251025
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)