Skip to content

Commit f5409cb

Browse files
authored
REF: call _maybe_cast_indexer upfront, better exception messages (#31638)
1 parent bacd48b commit f5409cb

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

pandas/core/indexes/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2888,10 +2888,11 @@ def get_loc(self, key, method=None, tolerance=None):
28882888
"tolerance argument only valid if using pad, "
28892889
"backfill or nearest lookups"
28902890
)
2891+
casted_key = self._maybe_cast_indexer(key)
28912892
try:
2892-
return self._engine.get_loc(key)
2893+
return self._engine.get_loc(casted_key)
28932894
except KeyError:
2894-
return self._engine.get_loc(self._maybe_cast_indexer(key))
2895+
raise KeyError(key)
28952896

28962897
if tolerance is not None:
28972898
tolerance = self._convert_tolerance(tolerance, np.asarray(key))

pandas/tests/indexes/multi/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_get_loc_missing_nan():
392392
# GH 8569
393393
idx = MultiIndex.from_arrays([[1.0, 2.0], [3.0, 4.0]])
394394
assert isinstance(idx.get_loc(1), slice)
395-
with pytest.raises(KeyError, match=r"^3\.0$"):
395+
with pytest.raises(KeyError, match=r"^3$"):
396396
idx.get_loc(3)
397397
with pytest.raises(KeyError, match=r"^nan$"):
398398
idx.get_loc(np.nan)

pandas/tests/indexes/test_numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def test_get_loc_missing_nan(self):
385385
# GH 8569
386386
idx = Float64Index([1, 2])
387387
assert idx.get_loc(1) == 0
388-
with pytest.raises(KeyError, match=r"^3\.0$"):
388+
with pytest.raises(KeyError, match=r"^3$"):
389389
idx.get_loc(3)
390390
with pytest.raises(KeyError, match="^nan$"):
391391
idx.get_loc(np.nan)

pandas/tests/indexing/test_floats.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_scalar_non_numeric(self):
107107
"mixed",
108108
}:
109109
error = KeyError
110-
msg = r"^3$"
110+
msg = r"^3\.0$"
111111
else:
112112
error = TypeError
113113
msg = (
@@ -187,7 +187,7 @@ def test_scalar_with_mixed(self):
187187
with pytest.raises(TypeError, match=msg):
188188
idxr(s2)[1.0]
189189

190-
with pytest.raises(KeyError, match=r"^1$"):
190+
with pytest.raises(KeyError, match=r"^1\.0$"):
191191
s2.loc[1.0]
192192

193193
result = s2.loc["b"]
@@ -213,7 +213,7 @@ def test_scalar_with_mixed(self):
213213
msg = "Cannot index by location index with a non-integer key"
214214
with pytest.raises(TypeError, match=msg):
215215
s3.iloc[1.0]
216-
with pytest.raises(KeyError, match=r"^1$"):
216+
with pytest.raises(KeyError, match=r"^1\.0$"):
217217
s3.loc[1.0]
218218

219219
result = s3.loc[1.5]
@@ -666,11 +666,11 @@ def test_floating_misc(self):
666666
# value not found (and no fallbacking at all)
667667

668668
# scalar integers
669-
with pytest.raises(KeyError, match=r"^4\.0$"):
669+
with pytest.raises(KeyError, match=r"^4$"):
670670
s.loc[4]
671-
with pytest.raises(KeyError, match=r"^4\.0$"):
671+
with pytest.raises(KeyError, match=r"^4$"):
672672
s.loc[4]
673-
with pytest.raises(KeyError, match=r"^4\.0$"):
673+
with pytest.raises(KeyError, match=r"^4$"):
674674
s[4]
675675

676676
# fancy floats/integers create the correct entry (as nan)

0 commit comments

Comments
 (0)