Skip to content
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

BUG/API: allow get_indexer to work with nans #7855

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,12 @@ def isin(self, values):
return lib.ismember_nans(self._array_values(), value_set,
isnull(list(value_set)).any())

def get_indexer(self, values, method=None, limit=None):
result = super(Float64Index, self).get_indexer(values, method=method,
limit=limit)
result[result == -1] = self._nan_idxs
return result


class MultiIndex(Index):

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,18 @@ def test_astype_from_object(self):
tm.assert_equal(result.dtype, expected.dtype)
tm.assert_index_equal(result, expected)

def test_get_indexer_nans(self):
index = Index([1, 2, np.nan])
result = index.get_indexer([np.nan])
np.testing.assert_array_equal(result, np.array([2]))

index = Index([1, np.nan, 2, np.nan])
with tm.assertRaisesRegexp(InvalidIndexError, 'Reindexing.*valid.*'):
index.get_indexer([np.nan])

index = Index([1, np.nan, 2])
result = index.get_indexer([np.nan, np.nan])
np.testing.assert_array_equal(result, np.array([1, 1]))

class TestInt64Index(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down