Skip to content

REF: call _maybe_cast_indexer upfront, better exception messages #31638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2888,10 +2888,11 @@ def get_loc(self, key, method=None, tolerance=None):
"tolerance argument only valid if using pad, "
"backfill or nearest lookups"
)
casted_key = self._maybe_cast_indexer(key)
try:
return self._engine.get_loc(key)
return self._engine.get_loc(casted_key)
except KeyError:
return self._engine.get_loc(self._maybe_cast_indexer(key))
raise KeyError(key)

if tolerance is not None:
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def test_get_loc_missing_nan():
# GH 8569
idx = MultiIndex.from_arrays([[1.0, 2.0], [3.0, 4.0]])
assert isinstance(idx.get_loc(1), slice)
with pytest.raises(KeyError, match=r"^3\.0$"):
with pytest.raises(KeyError, match=r"^3$"):
idx.get_loc(3)
with pytest.raises(KeyError, match=r"^nan$"):
idx.get_loc(np.nan)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def test_get_loc_missing_nan(self):
# GH 8569
idx = Float64Index([1, 2])
assert idx.get_loc(1) == 0
with pytest.raises(KeyError, match=r"^3\.0$"):
with pytest.raises(KeyError, match=r"^3$"):
idx.get_loc(3)
with pytest.raises(KeyError, match="^nan$"):
idx.get_loc(np.nan)
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_scalar_non_numeric(self):
"mixed",
}:
error = KeyError
msg = r"^3$"
msg = r"^3\.0$"
else:
error = TypeError
msg = (
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_scalar_with_mixed(self):
with pytest.raises(TypeError, match=msg):
idxr(s2)[1.0]

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

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

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

# scalar integers
with pytest.raises(KeyError, match=r"^4\.0$"):
with pytest.raises(KeyError, match=r"^4$"):
s.loc[4]
with pytest.raises(KeyError, match=r"^4\.0$"):
with pytest.raises(KeyError, match=r"^4$"):
s.loc[4]
with pytest.raises(KeyError, match=r"^4\.0$"):
with pytest.raises(KeyError, match=r"^4$"):
s[4]

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