From 89a5a4382317a39399f76b16c63e4fcf321ee0f7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 23 Sep 2019 17:49:18 -0700 Subject: [PATCH] CLN: indexing Exception in Series --- pandas/core/series.py | 8 ++++++-- pandas/tests/indexing/test_indexing.py | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 2431bfcfd0356..c87e371354f63 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1131,7 +1131,9 @@ def _get_with(self, key): elif isinstance(key, tuple): try: return self._get_values_tuple(key) - except Exception: + except ValueError: + # if we don't have a MultiIndex, we may still be able to handle + # a 1-tuple. see test_1tuple_without_multiindex if len(key) == 1: key = key[0] if isinstance(key, slice): @@ -1186,7 +1188,9 @@ def _get_values(self, indexer): return self._constructor( self._data.get_slice(indexer), fastpath=True ).__finalize__(self) - except Exception: + except ValueError: + # mpl compat if we look up e.g. ser[:, np.newaxis]; + # see tests.series.timeseries.test_mpl_compat_hack return self._values[indexer] def _get_value(self, label, takeable: bool = False): diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index e375bd459e66f..d478fbfa1686d 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -1202,3 +1202,12 @@ def test_readonly_indices(): result = df["data"].iloc[indices] expected = df["data"].loc[[1, 3, 6]] tm.assert_series_equal(result, expected) + + +def test_1tuple_without_multiindex(): + ser = pd.Series(range(5)) + key = (slice(3),) + + result = ser[key] + expected = ser[key[0]] + tm.assert_series_equal(result, expected)