Skip to content

Make get_loc with nan for FloatIndex consistent with other index types #39382

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 10 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_loc(self, key, method=None, tolerance=None):
# Catch this to avoid accidentally casting to 1.0
raise KeyError(key)

if is_float(key) and np.isnan(key):
if is_float(key) and np.isnan(key) and method is None:
nan_idxs = self._nan_idxs
if not len(nan_idxs):
raise KeyError(key)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def test_get_loc_missing_nan(self):
# listlike/non-hashable raises TypeError
idx.get_loc([np.nan])

@pytest.mark.parametrize("method", ["nearest", "pad"])
def test_get_loc_float_index_nan_with_method(self, method):
# GH#
idx = Index([1.0, 2.0, 3.0])
result = idx.get_loc(np.nan, method=method)
assert result == 2


class TestGetIndexer:
@pytest.mark.parametrize("method", ["pad", "backfill", "nearest"])
Expand Down