Skip to content

Commit b0d7859

Browse files
committed
Merge pull request #4837 from jtratner/fix-tuple-index-series-exception
ENH: Better Exception when trying to set on Series with tuple-index.
2 parents d273893 + 445bc89 commit b0d7859

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Improvements to existing features
118118
thanks @lgautier
119119
- DataFrame constructor now accepts a numpy masked record array (:issue:`3478`),
120120
thanks @jnothman
121+
- ``__getitem__`` with ``tuple`` key (e.g., ``[:, 2]``) on ``Series``
122+
without ``MultiIndex`` raises ``ValueError`` (:issue:`4759`, :issue:`4837`)
121123

122124
API Changes
123125
~~~~~~~~~~~

pandas/core/series.py

+2
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,8 @@ def __setitem__(self, key, value):
10501050
return
10511051

10521052
except TypeError as e:
1053+
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
1054+
raise ValueError("Can only tuple-index with a MultiIndex")
10531055
# python 3 type errors should be raised
10541056
if 'unorderable' in str(e): # pragma: no cover
10551057
raise IndexError(key)

pandas/tests/test_series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,10 @@ def test_setslice(self):
10251025

10261026
def test_basic_getitem_setitem_corner(self):
10271027
# invalid tuples, e.g. self.ts[:, None] vs. self.ts[:, 2]
1028-
self.assertRaises(Exception, self.ts.__getitem__,
1029-
(slice(None, None), 2))
1030-
self.assertRaises(Exception, self.ts.__setitem__,
1031-
(slice(None, None), 2), 2)
1028+
with tm.assertRaisesRegexp(ValueError, 'tuple-index'):
1029+
self.ts[:, 2]
1030+
with tm.assertRaisesRegexp(ValueError, 'tuple-index'):
1031+
self.ts[:, 2] = 2
10321032

10331033
# weird lists. [slice(0, 5)] will work but not two slices
10341034
result = self.ts[[slice(None, 5)]]

0 commit comments

Comments
 (0)