Skip to content

Commit bd23ab4

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

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

pandas/core/indexing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ def _get_setitem_indexer(self, key):
652652
return ax.get_loc(key)
653653

654654
if isinstance(key, tuple):
655-
with suppress(IndexingError):
656-
return self._convert_tuple(key)
655+
return self._convert_tuple(key)
657656

658657
if isinstance(key, range):
659658
return list(key)
@@ -911,7 +910,7 @@ def _getitem_nested_tuple(self, tup: tuple):
911910
# we are only getting non-hashable tuples, in particular ones
912911
# that themselves contain a slice entry
913912
# See test_loc_series_getitem_too_many_dimensions
914-
raise ValueError("Too many indices")
913+
raise IndexingError("Too many indexers")
915914

916915
# this is a series with a multi-index specified a tuple of
917916
# selectors

pandas/tests/indexing/test_loc.py

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

27172717

2718+
def test_loc_setitem_indexer_length():
2719+
df = DataFrame({"a": [10]})
2720+
msg = "Too many indexers"
2721+
with pytest.raises(IndexingError, match=msg):
2722+
df["a"].loc[0, 0] = 1000
2723+
2724+
27182725
class TestLocSeries:
27192726
@pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)])
27202727
def test_loc_uint64(self, val, expected):
@@ -2889,11 +2896,11 @@ def test_loc_series_getitem_too_many_dimensions(self, indexer):
28892896
index=MultiIndex.from_tuples([("A", "0"), ("A", "1"), ("B", "0")]),
28902897
data=[21, 22, 23],
28912898
)
2892-
msg = "Too many indices"
2893-
with pytest.raises(ValueError, match=msg):
2899+
msg = "Too many indexers"
2900+
with pytest.raises(IndexingError, match=msg):
28942901
ser.loc[indexer, :]
28952902

2896-
with pytest.raises(ValueError, match=msg):
2903+
with pytest.raises(IndexingError, match=msg):
28972904
ser.loc[indexer, :] = 1
28982905

28992906
def test_loc_setitem(self, string_series):

0 commit comments

Comments
 (0)