Skip to content

Commit 87b5153

Browse files
jbrockmendeljreback
authored andcommitted
CLN: indexing Exception in Series (#28588)
1 parent 0971aaf commit 87b5153

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pandas/core/series.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,9 @@ def _get_with(self, key):
11311131
elif isinstance(key, tuple):
11321132
try:
11331133
return self._get_values_tuple(key)
1134-
except Exception:
1134+
except ValueError:
1135+
# if we don't have a MultiIndex, we may still be able to handle
1136+
# a 1-tuple. see test_1tuple_without_multiindex
11351137
if len(key) == 1:
11361138
key = key[0]
11371139
if isinstance(key, slice):
@@ -1186,7 +1188,9 @@ def _get_values(self, indexer):
11861188
return self._constructor(
11871189
self._data.get_slice(indexer), fastpath=True
11881190
).__finalize__(self)
1189-
except Exception:
1191+
except ValueError:
1192+
# mpl compat if we look up e.g. ser[:, np.newaxis];
1193+
# see tests.series.timeseries.test_mpl_compat_hack
11901194
return self._values[indexer]
11911195

11921196
def _get_value(self, label, takeable: bool = False):

pandas/tests/indexing/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,12 @@ def test_readonly_indices():
12021202
result = df["data"].iloc[indices]
12031203
expected = df["data"].loc[[1, 3, 6]]
12041204
tm.assert_series_equal(result, expected)
1205+
1206+
1207+
def test_1tuple_without_multiindex():
1208+
ser = pd.Series(range(5))
1209+
key = (slice(3),)
1210+
1211+
result = ser[key]
1212+
expected = ser[key[0]]
1213+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)