Skip to content

CLN/API: clean-up of level argument of isin for indexes #26635

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

Closed
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
9 changes: 6 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4649,7 +4649,7 @@ def map(self, mapper, na_action=None):

return Index(new_values, **attributes)

def isin(self, values, level=None):
def isin(self, values):
"""
Return a boolean array where the index values are in `values`.

Expand All @@ -4675,6 +4675,11 @@ def isin(self, values, level=None):
is_contained : ndarray
NumPy array of boolean values.

Raises
------
TypeError
If `level` is passed and the index is not a `MultiIndex`.

See Also
--------
Series.isin : Same for Series.
Expand Down Expand Up @@ -4733,8 +4738,6 @@ def isin(self, values, level=None):
>>> dti.isin(['2000-03-11'])
array([ True, False, False])
"""
if level is not None:
self._validate_index_level(level)
return algos.isin(self, values)

def _get_string_slice(self, key, use_lhs=True, use_rhs=True):
Expand Down
15 changes: 2 additions & 13 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,19 +509,8 @@ def __rsub__(self, other):

cls.__rsub__ = __rsub__

def isin(self, values, level=None):
"""
Compute boolean array of whether each index value is found in the
passed set of values.

Parameters
----------
values : set or sequence of values

Returns
-------
is_contained : ndarray (boolean dtype)
"""
@Appender(Index.isin.__doc__)
def isin(self, values):
if not isinstance(values, type(self)):
try:
values = type(self)(values)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ def is_unique(self):
return super().is_unique and self._nan_idxs.size < 2

@Appender(Index.isin.__doc__)
def isin(self, values, level=None):
if level is not None:
self._validate_index_level(level)
def isin(self, values):
return algorithms.isin(np.array(self), values)


Expand Down
25 changes: 7 additions & 18 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,36 +1776,25 @@ def test_isin_nan_common_float64(self, nulls_fixture):
tm.assert_numpy_array_equal(Float64Index([1.0, nulls_fixture]).isin(
[pd.NaT]), np.array([False, False]))

@pytest.mark.parametrize("level", [0, -1])
@pytest.mark.parametrize("index", [
Index(['qux', 'baz', 'foo', 'bar']),
# Float64Index overrides isin, so must be checked separately
Float64Index([1.0, 2.0, 3.0, 4.0])])
def test_isin_level_kwarg(self, level, index):
def test_isin_nonexisting(self, index):
values = index.tolist()[-2:] + ['nonexisting']

expected = np.array([False, False, True, True])
tm.assert_numpy_array_equal(expected, index.isin(values, level=level))
tm.assert_numpy_array_equal(expected, index.isin(values))

index.name = 'foobar'
tm.assert_numpy_array_equal(expected,
index.isin(values, level='foobar'))

@pytest.mark.parametrize("level", [1, 10, -2])
@pytest.mark.parametrize("level", [
1, 10, -2, 1.0, 'foobar', 'xyzzy', np.nan])
@pytest.mark.parametrize("index", [
Index(['qux', 'baz', 'foo', 'bar']),
# Float64Index overrides isin, so must be checked separately
Float64Index([1.0, 2.0, 3.0, 4.0])])
def test_isin_level_kwarg_raises_bad_index(self, level, index):
with pytest.raises(IndexError, match='Too many levels'):
index.isin([], level=level)

@pytest.mark.parametrize("level", [1.0, 'foobar', 'xyzzy', np.nan])
@pytest.mark.parametrize("index", [
Index(['qux', 'baz', 'foo', 'bar']),
Float64Index([1.0, 2.0, 3.0, 4.0])])
def test_isin_level_kwarg_raises_key(self, level, index):
with pytest.raises(KeyError, match='must be same as name'):
def test_isin_level_kwarg_raises(self, level, index):
msg = r"isin\(\) got an unexpected keyword argument 'level'"
with pytest.raises(TypeError, match=msg):
index.isin([], level=level)

@pytest.mark.parametrize("empty", [[], Series(), np.array([])])
Expand Down