Skip to content

Commit ca37c9c

Browse files
BUG: fix .loc.__setitem__ not raising when using too many indexers
1 parent 6b53d07 commit ca37c9c

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

pandas/core/indexing.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ def _getitem_nested_tuple(self, tup: tuple):
911911
# we are only getting non-hashable tuples, in particular ones
912912
# that themselves contain a slice entry
913913
# See test_loc_series_getitem_too_many_dimensions
914-
raise ValueError("Too many indices")
914+
raise IndexingError("Too many indexers")
915915

916916
# this is a series with a multi-index specified a tuple of
917917
# selectors
@@ -1231,6 +1231,11 @@ def _convert_to_indexer(self, key, axis: int):
12311231
is_int_index = labels.is_integer()
12321232
is_int_positional = is_integer(key) and not is_int_index
12331233

1234+
if isinstance(key, tuple) and not isinstance(labels, MultiIndex):
1235+
if len(key) > 1:
1236+
raise IndexingError("Too many indexers")
1237+
key = key[0]
1238+
12341239
if is_scalar(key) or (isinstance(labels, MultiIndex) and is_hashable(key)):
12351240
# Otherwise get_loc will raise InvalidIndexError
12361241

@@ -1262,7 +1267,7 @@ def _convert_to_indexer(self, key, axis: int):
12621267
if is_nested_tuple(key, labels):
12631268
if self.ndim == 1 and any(isinstance(k, tuple) for k in key):
12641269
# GH#35349 Raise if tuple in tuple for series
1265-
raise ValueError("Too many indices")
1270+
raise IndexingError("Too many indexers")
12661271
return labels.get_locs(key)
12671272

12681273
elif is_list_like_indexer(key):

pandas/tests/indexing/test_iloc.py

+9
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,15 @@ def test_iloc_getitem_setitem_fancy_exceptions(self, float_frame):
11891189
# GH#32257 we let numpy do validation, get their exception
11901190
float_frame.iloc[:, :, :] = 1
11911191

1192+
def test_iloc_setitem_indexer_length(self):
1193+
# GH#13831
1194+
ser = Series([10])
1195+
with pytest.raises(IndexError, match="too many indices for array"):
1196+
ser.iloc[0, 0] = 1000
1197+
1198+
with pytest.raises(IndexingError, match="Too many indexers"):
1199+
ser.iloc[0, 0]
1200+
11921201
# TODO(ArrayManager) "split" path doesn't properly implement DataFrame indexer
11931202
@td.skip_array_manager_not_yet_implemented
11941203
def test_iloc_frame_indexer(self):

pandas/tests/indexing/test_loc.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,17 @@ def test_loc_getitem_multiindex_tuple_level():
27152715
assert result2 == 6
27162716

27172717

2718+
def test_loc_setitem_indexer_length():
2719+
# GH#13831
2720+
ser = Series([10])
2721+
msg = "Too many indexers"
2722+
with pytest.raises(IndexingError, match=msg):
2723+
ser.loc[0, 0] = 1000
2724+
2725+
with pytest.raises(IndexingError, match=msg):
2726+
ser.loc[0, 0]
2727+
2728+
27182729
class TestLocSeries:
27192730
@pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)])
27202731
def test_loc_uint64(self, val, expected):
@@ -2889,11 +2900,11 @@ def test_loc_series_getitem_too_many_dimensions(self, indexer):
28892900
index=MultiIndex.from_tuples([("A", "0"), ("A", "1"), ("B", "0")]),
28902901
data=[21, 22, 23],
28912902
)
2892-
msg = "Too many indices"
2893-
with pytest.raises(ValueError, match=msg):
2903+
msg = "Too many indexers"
2904+
with pytest.raises(IndexingError, match=msg):
28942905
ser.loc[indexer, :]
28952906

2896-
with pytest.raises(ValueError, match=msg):
2907+
with pytest.raises(IndexingError, match=msg):
28972908
ser.loc[indexer, :] = 1
28982909

28992910
def test_loc_setitem(self, string_series):

0 commit comments

Comments
 (0)