-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: pd.MultiIndex.get_loc(np.nan) #28919
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2539,7 +2539,7 @@ def _partial_tup_index(self, tup, side="left"): | |
for k, (lab, lev, labs) in enumerate(zipped): | ||
section = labs[start:end] | ||
|
||
if lab not in lev: | ||
if lab not in lev and not isna(lab): | ||
if not lev.is_type_compatible(lib.infer_dtype([lab], skipna=False)): | ||
raise TypeError(f"Level type mismatch: {lab}") | ||
|
||
|
@@ -2549,13 +2549,38 @@ def _partial_tup_index(self, tup, side="left"): | |
loc -= 1 | ||
return start + section.searchsorted(loc, side=side) | ||
|
||
idx = lev.get_loc(lab) | ||
idx = self._get_loc_single_level_index(lev, lab) | ||
if k < n - 1: | ||
end = start + section.searchsorted(idx, side="right") | ||
start = start + section.searchsorted(idx, side="left") | ||
else: | ||
return start + section.searchsorted(idx, side=side) | ||
|
||
def _get_loc_single_level_index(self, level_index: Index, key: Hashable) -> int: | ||
""" | ||
If key is NA value, location of index unify as -1. | ||
|
||
Parameters | ||
---------- | ||
level_index: Index | ||
key : label | ||
|
||
Returns | ||
------- | ||
loc : int | ||
If key is NA value, loc is -1 | ||
Else, location of key in index. | ||
|
||
See Also | ||
-------- | ||
Index.get_loc : The get_loc method for (single-level) index. | ||
""" | ||
|
||
if is_scalar(key) and isna(key): | ||
return -1 | ||
else: | ||
return level_index.get_loc(key) | ||
|
||
def get_loc(self, key, method=None): | ||
""" | ||
Get location for a label or a tuple of labels as an integer, slice or | ||
|
@@ -2654,7 +2679,9 @@ def _maybe_to_slice(loc): | |
loc = np.arange(start, stop, dtype="int64") | ||
|
||
for i, k in enumerate(follow_key, len(lead_key)): | ||
mask = self.codes[i][loc] == self.levels[i].get_loc(k) | ||
mask = self.codes[i][loc] == self._get_loc_single_level_index( | ||
self.levels[i], k | ||
) | ||
if not mask.all(): | ||
loc = loc[mask] | ||
if not len(loc): | ||
|
@@ -2882,7 +2909,7 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes): | |
|
||
else: | ||
|
||
code = level_index.get_loc(key) | ||
code = self._get_loc_single_level_index(level_index, key) | ||
|
||
if level > 0 or self.lexsort_depth == 0: | ||
# Desired level is not sorted | ||
|
@@ -3377,14 +3404,11 @@ def isin(self, values, level=None): | |
return algos.isin(self.values, values) | ||
else: | ||
num = self._get_level_number(level) | ||
levs = self.levels[num] | ||
level_codes = self.codes[num] | ||
levs = self.get_level_values(num) | ||
|
||
sought_labels = levs.isin(values).nonzero()[0] | ||
if levs.size == 0: | ||
return np.zeros(len(level_codes), dtype=np.bool_) | ||
else: | ||
return np.lib.arraysetops.in1d(level_codes, sought_labels) | ||
return np.zeros(len(levs), dtype=np.bool_) | ||
return levs.isin(values) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the edits from L3408 down to here look like they are just nice cleanups independent of the rest of this PR. is that accurate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbrockmendel |
||
|
||
|
||
MultiIndex._add_numeric_methods_disabled() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,3 +98,27 @@ def test_isin_level_kwarg(): | |
|
||
with pytest.raises(KeyError, match="'Level C not found'"): | ||
idx.isin(vals_1, level="C") | ||
|
||
|
||
def test_contains_with_missing_value(): | ||
# issue 19132 | ||
idx = MultiIndex.from_arrays([[1, np.nan, 2]]) | ||
assert np.nan in idx | ||
|
||
idx = MultiIndex.from_arrays([[1, 2], [np.nan, 3]]) | ||
assert np.nan not in idx | ||
assert (1, np.nan) in idx | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"labels,expected,level", | ||
[ | ||
([("b", np.nan)], np.array([False, False, True]), None,), | ||
([np.nan, "a"], np.array([True, True, False]), 0), | ||
(["d", np.nan], np.array([False, True, True]), 1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the issue specific to np.nan, or are there other NA values worth testing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbrockmendel |
||
], | ||
) | ||
def test_isin_multi_index_with_missing_value(labels, expected, level): | ||
# GH 19132 | ||
midx = MultiIndex.from_arrays([[np.nan, "a", "b"], ["c", "d", np.nan]]) | ||
tm.assert_numpy_array_equal(midx.isin(labels, level=level), expected) |
Uh oh!
There was an error while loading. Please reload this page.