Skip to content

Commit 71f40a6

Browse files
authored
ERR: Improve error message for Series.loc.getitem with too many dimensions (#39372)
1 parent d0cfa03 commit 71f40a6

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Other enhancements
5454
- :meth:`Series.apply` can now accept list-like or dictionary-like arguments that aren't lists or dictionaries, e.g. ``ser.apply(np.array(["sum", "mean"]))``, which was already the case for :meth:`DataFrame.apply` (:issue:`39140`)
5555
- :meth:`DataFrame.plot.scatter` can now accept a categorical column as the argument to ``c`` (:issue:`12380`, :issue:`31357`)
5656
- :meth:`.Styler.set_tooltips` allows on hover tooltips to be added to styled HTML dataframes.
57+
- :meth:`Series.loc.__getitem__` and :meth:`Series.loc.__setitem__` with :class:`MultiIndex` now raising helpful error message when indexer has too many dimensions (:issue:`35349`)
5758

5859
.. ---------------------------------------------------------------------------
5960

pandas/core/indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,11 @@ def _getitem_nested_tuple(self, tup: Tuple):
846846
if self.name != "loc":
847847
# This should never be reached, but lets be explicit about it
848848
raise ValueError("Too many indices")
849+
if isinstance(self.obj, ABCSeries) and any(
850+
isinstance(k, tuple) for k in tup
851+
):
852+
# GH#35349 Raise if tuple in tuple for series
853+
raise ValueError("Too many indices")
849854
if self.ndim == 1 or not any(isinstance(x, slice) for x in tup):
850855
# GH#10521 Series should reduce MultiIndex dimensions instead of
851856
# DataFrame, IndexingError is not raised when slice(None,None,None)
@@ -1203,6 +1208,11 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
12031208
return {"key": key}
12041209

12051210
if is_nested_tuple(key, labels):
1211+
if isinstance(self.obj, ABCSeries) and any(
1212+
isinstance(k, tuple) for k in key
1213+
):
1214+
# GH#35349 Raise if tuple in tuple for series
1215+
raise ValueError("Too many indices")
12061216
return labels.get_locs(key)
12071217

12081218
elif is_list_like_indexer(key):

pandas/tests/indexing/test_loc.py

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
DataFrame,
1818
DatetimeIndex,
1919
Index,
20+
IndexSlice,
2021
MultiIndex,
2122
Series,
2223
SparseDtype,
@@ -2130,3 +2131,17 @@ def test_loc_iloc_setitem_with_listlike(self, size, array_fn):
21302131
ser = Series(0, index=list("abcde"), dtype=object)
21312132
ser.iloc[0] = arr
21322133
tm.assert_series_equal(ser, expected)
2134+
2135+
@pytest.mark.parametrize("indexer", [IndexSlice["A", :], ("A", slice(None))])
2136+
def test_loc_series_getitem_too_many_dimensions(self, indexer):
2137+
# GH#35349
2138+
ser = Series(
2139+
index=MultiIndex.from_tuples([("A", "0"), ("A", "1"), ("B", "0")]),
2140+
data=[21, 22, 23],
2141+
)
2142+
msg = "Too many indices"
2143+
with pytest.raises(ValueError, match=msg):
2144+
ser.loc[indexer, :]
2145+
2146+
with pytest.raises(ValueError, match=msg):
2147+
ser.loc[indexer, :] = 1

0 commit comments

Comments
 (0)