Skip to content

Commit ca84bd0

Browse files
ERR: improve error message for invalid indexer (#31769)
1 parent 0bfb8cb commit ca84bd0

File tree

8 files changed

+51
-62
lines changed

8 files changed

+51
-62
lines changed

pandas/core/indexes/base.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3158,9 +3158,9 @@ def _convert_slice_indexer(self, key: slice, kind=None):
31583158

31593159
# validate iloc
31603160
if kind == "iloc":
3161-
self._validate_indexer("slice", key.start, "iloc")
3162-
self._validate_indexer("slice", key.stop, "iloc")
3163-
self._validate_indexer("slice", key.step, "iloc")
3161+
self._validate_indexer("positional", key.start, "iloc")
3162+
self._validate_indexer("positional", key.stop, "iloc")
3163+
self._validate_indexer("positional", key.step, "iloc")
31643164
return key
31653165

31663166
# potentially cast the bounds to integers
@@ -3285,8 +3285,8 @@ def _invalid_indexer(self, form: str_t, key):
32853285
Consistent invalid indexer message.
32863286
"""
32873287
raise TypeError(
3288-
f"cannot do {form} indexing on {type(self)} with these "
3289-
f"indexers [{key}] of {type(key)}"
3288+
f"cannot do {form} indexing on {type(self).__name__} with these "
3289+
f"indexers [{key}] of type {type(key).__name__}"
32903290
)
32913291

32923292
# --------------------------------------------------------------------

pandas/core/indexes/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ def _convert_scalar_indexer(self, key, kind: str):
406406
is_int = is_integer(key)
407407
is_flt = is_float(key)
408408
if kind == "loc" and (is_int or is_flt):
409-
self._invalid_indexer("index", key)
409+
self._invalid_indexer("label", key)
410410
elif kind == "getitem" and is_flt:
411-
self._invalid_indexer("index", key)
411+
self._invalid_indexer("label", key)
412412

413413
return super()._convert_scalar_indexer(key, kind=kind)
414414

pandas/tests/frame/indexing/test_indexing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,8 @@ def test_getitem_setitem_float_labels(self):
10501050

10511051
# positional slicing only via iloc!
10521052
msg = (
1053-
"cannot do slice indexing on "
1054-
r"<class 'pandas\.core\.indexes\.numeric\.Float64Index'> with "
1055-
r"these indexers \[1.0\] of <class 'float'>"
1053+
"cannot do positional indexing on Float64Index with "
1054+
r"these indexers \[1.0\] of type float"
10561055
)
10571056
with pytest.raises(TypeError, match=msg):
10581057
df.iloc[1.0:5]

pandas/tests/frame/test_constructors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1860,9 +1860,8 @@ def check(df):
18601860
# No NaN found -> error
18611861
if len(indexer) == 0:
18621862
msg = (
1863-
"cannot do label indexing on "
1864-
r"<class 'pandas\.core\.indexes\.range\.RangeIndex'> "
1865-
r"with these indexers \[nan\] of <class 'float'>"
1863+
"cannot do label indexing on RangeIndex "
1864+
r"with these indexers \[nan\] of type float"
18661865
)
18671866
with pytest.raises(TypeError, match=msg):
18681867
df.loc[:, np.nan]

pandas/tests/indexing/test_categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def test_loc_scalar(self):
8383
df.loc["d", "C"] = 10
8484

8585
msg = (
86-
r"cannot do label indexing on <class 'pandas\.core\.indexes\.category"
87-
r"\.CategoricalIndex'> with these indexers \[1\] of <class 'int'>"
86+
"cannot do label indexing on CategoricalIndex with these "
87+
r"indexers \[1\] of type int"
8888
)
8989
with pytest.raises(TypeError, match=msg):
9090
df.loc[1]

pandas/tests/indexing/test_floats.py

+32-40
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_scalar_error(self, index_func):
5454

5555
msg = (
5656
"cannot do positional indexing on {klass} with these "
57-
r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float))
57+
r"indexers \[3\.0\] of type float".format(klass=type(i).__name__)
5858
)
5959
with pytest.raises(TypeError, match=msg):
6060
s.iloc[3.0] = 0
@@ -92,11 +92,11 @@ def test_scalar_non_numeric(self):
9292
else:
9393
error = TypeError
9494
msg = (
95-
r"cannot do (label|index|positional) indexing "
95+
r"cannot do (label|positional) indexing "
9696
r"on {klass} with these indexers \[3\.0\] of "
97-
r"{kind}|"
97+
r"type float|"
9898
"Cannot index by location index with a "
99-
"non-integer key".format(klass=type(i), kind=str(float))
99+
"non-integer key".format(klass=type(i).__name__)
100100
)
101101
with pytest.raises(error, match=msg):
102102
idxr(s)[3.0]
@@ -113,9 +113,9 @@ def test_scalar_non_numeric(self):
113113
else:
114114
error = TypeError
115115
msg = (
116-
r"cannot do (label|index) indexing "
116+
r"cannot do label indexing "
117117
r"on {klass} with these indexers \[3\.0\] of "
118-
r"{kind}".format(klass=type(i), kind=str(float))
118+
r"type float".format(klass=type(i).__name__)
119119
)
120120
with pytest.raises(error, match=msg):
121121
s.loc[3.0]
@@ -125,9 +125,9 @@ def test_scalar_non_numeric(self):
125125

126126
# setting with a float fails with iloc
127127
msg = (
128-
r"cannot do (label|index|positional) indexing "
128+
r"cannot do (label|positional) indexing "
129129
r"on {klass} with these indexers \[3\.0\] of "
130-
r"{kind}".format(klass=type(i), kind=str(float))
130+
r"type float".format(klass=type(i).__name__)
131131
)
132132
with pytest.raises(TypeError, match=msg):
133133
s.iloc[3.0] = 0
@@ -162,9 +162,9 @@ def test_scalar_non_numeric(self):
162162
s = Series(np.arange(len(i)), index=i)
163163
s[3]
164164
msg = (
165-
r"cannot do (label|index) indexing "
165+
r"cannot do label indexing "
166166
r"on {klass} with these indexers \[3\.0\] of "
167-
r"{kind}".format(klass=type(i), kind=str(float))
167+
r"type float".format(klass=type(i).__name__)
168168
)
169169
with pytest.raises(TypeError, match=msg):
170170
s[3.0]
@@ -181,9 +181,9 @@ def test_scalar_with_mixed(self):
181181
msg = (
182182
r"cannot do label indexing "
183183
r"on {klass} with these indexers \[1\.0\] of "
184-
r"{kind}|"
184+
r"type float|"
185185
"Cannot index by location index with a non-integer key".format(
186-
klass=str(Index), kind=str(float)
186+
klass=Index.__name__
187187
)
188188
)
189189
with pytest.raises(TypeError, match=msg):
@@ -203,7 +203,7 @@ def test_scalar_with_mixed(self):
203203
msg = (
204204
r"cannot do label indexing "
205205
r"on {klass} with these indexers \[1\.0\] of "
206-
r"{kind}".format(klass=str(Index), kind=str(float))
206+
r"type float".format(klass=Index.__name__)
207207
)
208208
with pytest.raises(TypeError, match=msg):
209209
idxr(s3)[1.0]
@@ -317,7 +317,7 @@ def test_scalar_float(self):
317317
msg = (
318318
r"cannot do positional indexing "
319319
r"on {klass} with these indexers \[3\.0\] of "
320-
r"{kind}".format(klass=str(Float64Index), kind=str(float))
320+
r"type float".format(klass=Float64Index.__name__)
321321
)
322322
with pytest.raises(TypeError, match=msg):
323323
s2.iloc[3.0] = 0
@@ -346,24 +346,20 @@ def test_slice_non_numeric(self):
346346
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:
347347

348348
msg = (
349-
"cannot do slice indexing "
349+
"cannot do positional indexing "
350350
r"on {klass} with these indexers \[(3|4)\.0\] of "
351-
"{kind}".format(klass=type(index), kind=str(float))
351+
"type float".format(klass=type(index).__name__)
352352
)
353353
with pytest.raises(TypeError, match=msg):
354354
s.iloc[l]
355355

356356
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:
357357

358358
msg = (
359-
"cannot do slice indexing "
359+
"cannot do (slice|positional) indexing "
360360
r"on {klass} with these indexers "
361361
r"\[(3|4)(\.0)?\] "
362-
r"of ({kind_float}|{kind_int})".format(
363-
klass=type(index),
364-
kind_float=str(float),
365-
kind_int=str(int),
366-
)
362+
r"of type (float|int)".format(klass=type(index).__name__)
367363
)
368364
with pytest.raises(TypeError, match=msg):
369365
idxr(s)[l]
@@ -372,23 +368,19 @@ def test_slice_non_numeric(self):
372368
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:
373369

374370
msg = (
375-
"cannot do slice indexing "
371+
"cannot do positional indexing "
376372
r"on {klass} with these indexers \[(3|4)\.0\] of "
377-
"{kind}".format(klass=type(index), kind=str(float))
373+
"type float".format(klass=type(index).__name__)
378374
)
379375
with pytest.raises(TypeError, match=msg):
380376
s.iloc[l] = 0
381377

382378
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:
383379
msg = (
384-
"cannot do slice indexing "
380+
"cannot do (slice|positional) indexing "
385381
r"on {klass} with these indexers "
386382
r"\[(3|4)(\.0)?\] "
387-
r"of ({kind_float}|{kind_int})".format(
388-
klass=type(index),
389-
kind_float=str(float),
390-
kind_int=str(int),
391-
)
383+
r"of type (float|int)".format(klass=type(index).__name__)
392384
)
393385
with pytest.raises(TypeError, match=msg):
394386
idxr(s)[l] = 0
@@ -428,7 +420,7 @@ def test_slice_integer(self):
428420
msg = (
429421
"cannot do slice indexing "
430422
r"on {klass} with these indexers \[(3|4)\.0\] of "
431-
"{kind}".format(klass=type(index), kind=str(float))
423+
"type float".format(klass=type(index).__name__)
432424
)
433425
with pytest.raises(TypeError, match=msg):
434426
s[l]
@@ -452,7 +444,7 @@ def test_slice_integer(self):
452444
msg = (
453445
"cannot do slice indexing "
454446
r"on {klass} with these indexers \[-6\.0\] of "
455-
"{kind}".format(klass=type(index), kind=str(float))
447+
"type float".format(klass=type(index).__name__)
456448
)
457449
with pytest.raises(TypeError, match=msg):
458450
s[slice(-6.0, 6.0)]
@@ -478,7 +470,7 @@ def test_slice_integer(self):
478470
msg = (
479471
"cannot do slice indexing "
480472
r"on {klass} with these indexers \[(2|3)\.5\] of "
481-
"{kind}".format(klass=type(index), kind=str(float))
473+
"type float".format(klass=type(index).__name__)
482474
)
483475
with pytest.raises(TypeError, match=msg):
484476
s[l]
@@ -496,7 +488,7 @@ def test_slice_integer(self):
496488
msg = (
497489
"cannot do slice indexing "
498490
r"on {klass} with these indexers \[(3|4)\.0\] of "
499-
"{kind}".format(klass=type(index), kind=str(float))
491+
"type float".format(klass=type(index).__name__)
500492
)
501493
with pytest.raises(TypeError, match=msg):
502494
s[l] = 0
@@ -517,9 +509,9 @@ def test_integer_positional_indexing(self):
517509

518510
klass = RangeIndex
519511
msg = (
520-
"cannot do slice indexing "
512+
"cannot do (slice|positional) indexing "
521513
r"on {klass} with these indexers \[(2|4)\.0\] of "
522-
"{kind}".format(klass=str(klass), kind=str(float))
514+
"type float".format(klass=klass.__name__)
523515
)
524516
with pytest.raises(TypeError, match=msg):
525517
idxr(s)[l]
@@ -544,7 +536,7 @@ def f(idxr):
544536
msg = (
545537
"cannot do slice indexing "
546538
r"on {klass} with these indexers \[(0|1)\.0\] of "
547-
"{kind}".format(klass=type(index), kind=str(float))
539+
"type float".format(klass=type(index).__name__)
548540
)
549541
with pytest.raises(TypeError, match=msg):
550542
s[l]
@@ -559,7 +551,7 @@ def f(idxr):
559551
msg = (
560552
"cannot do slice indexing "
561553
r"on {klass} with these indexers \[-10\.0\] of "
562-
"{kind}".format(klass=type(index), kind=str(float))
554+
"type float".format(klass=type(index).__name__)
563555
)
564556
with pytest.raises(TypeError, match=msg):
565557
s[slice(-10.0, 10.0)]
@@ -578,7 +570,7 @@ def f(idxr):
578570
msg = (
579571
"cannot do slice indexing "
580572
r"on {klass} with these indexers \[0\.5\] of "
581-
"{kind}".format(klass=type(index), kind=str(float))
573+
"type float".format(klass=type(index).__name__)
582574
)
583575
with pytest.raises(TypeError, match=msg):
584576
s[l]
@@ -595,7 +587,7 @@ def f(idxr):
595587
msg = (
596588
"cannot do slice indexing "
597589
r"on {klass} with these indexers \[(3|4)\.0\] of "
598-
"{kind}".format(klass=type(index), kind=str(float))
590+
"type float".format(klass=type(index).__name__)
599591
)
600592
with pytest.raises(TypeError, match=msg):
601593
s[l] = 0

pandas/tests/indexing/test_scalar.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def test_series_at_raises_type_error(self):
140140
assert result == 1
141141

142142
msg = (
143-
"cannot do label indexing on <class 'pandas.core.indexes.base.Index'> "
144-
r"with these indexers \[0\] of <class 'int'>"
143+
"cannot do label indexing on Index "
144+
r"with these indexers \[0\] of type int"
145145
)
146146
with pytest.raises(TypeError, match=msg):
147147
ser.at[0]
@@ -157,8 +157,8 @@ def test_frame_raises_type_error(self):
157157
assert result == 1
158158

159159
msg = (
160-
"cannot do label indexing on <class 'pandas.core.indexes.base.Index'> "
161-
r"with these indexers \[0\] of <class 'int'>"
160+
"cannot do label indexing on Index "
161+
r"with these indexers \[0\] of type int"
162162
)
163163
with pytest.raises(TypeError, match=msg):
164164
df.at["a", 0]

pandas/tests/series/indexing/test_numeric.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ def test_setitem_float_labels():
128128

129129
def test_slice_float_get_set(datetime_series):
130130
msg = (
131-
r"cannot do slice indexing on <class 'pandas\.core\.indexes"
132-
r"\.datetimes\.DatetimeIndex'> with these indexers \[{key}\] "
133-
r"of <class 'float'>"
131+
"cannot do slice indexing on DatetimeIndex with these indexers "
132+
r"\[{key}\] of type float"
134133
)
135134
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
136135
datetime_series[4.0:10.0]

0 commit comments

Comments
 (0)