diff --git a/doc/source/release.rst b/doc/source/release.rst index 791fbc2c516b5..62ca11b6cef0b 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -118,6 +118,8 @@ Improvements to existing features thanks @lgautier - DataFrame constructor now accepts a numpy masked record array (:issue:`3478`), thanks @jnothman + - ``__getitem__`` with ``tuple`` key (e.g., ``[:, 2]``) on ``Series`` + without ``MultiIndex`` raises ``ValueError`` (:issue:`4759`, :issue:`4837`) API Changes ~~~~~~~~~~~ diff --git a/pandas/core/series.py b/pandas/core/series.py index 8d6591c3acd60..7d3cce8c80f72 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1050,6 +1050,8 @@ def __setitem__(self, key, value): return except TypeError as e: + if isinstance(key, tuple) and not isinstance(self.index, MultiIndex): + raise ValueError("Can only tuple-index with a MultiIndex") # python 3 type errors should be raised if 'unorderable' in str(e): # pragma: no cover raise IndexError(key) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index d2d0bc39fbfc9..c52fcad3d5111 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1025,10 +1025,10 @@ def test_setslice(self): def test_basic_getitem_setitem_corner(self): # invalid tuples, e.g. self.ts[:, None] vs. self.ts[:, 2] - self.assertRaises(Exception, self.ts.__getitem__, - (slice(None, None), 2)) - self.assertRaises(Exception, self.ts.__setitem__, - (slice(None, None), 2), 2) + with tm.assertRaisesRegexp(ValueError, 'tuple-index'): + self.ts[:, 2] + with tm.assertRaisesRegexp(ValueError, 'tuple-index'): + self.ts[:, 2] = 2 # weird lists. [slice(0, 5)] will work but not two slices result = self.ts[[slice(None, 5)]]