Skip to content

API: change IntervalIndex.contains to work elementwise #17753

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
Jul 1, 2019
4 changes: 4 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4036,6 +4036,10 @@ def contains(self, key):
.. deprecated:: 0.25.0
Use ``key in index`` instead of ``index.contains(key)``.
"""
warnings.warn(
"The 'contains' method is deprecated and will be removed in a "
"future versions. Use 'key in index' instead of "
"'index.contains(key)", FutureWarning, stacklevel=2)
return key in self

def __hash__(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ def convert_to_index_sliceable(obj, key):
elif isinstance(key, str):

# we are an actual column
if obj._data.items.contains(key):
if key in obj._data.items:
return None

# We might have a datetimelike string that we can translate to a
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,11 @@ def test_tab_complete_warning(self, ip):
with provisionalcompleter('ignore'):
list(ip.Completer.completions('idx.', 4))

def test_deprecated_contains(self):
for index in self.indices.values():
with tm.assert_produces_warning(FutureWarning):
index.contains(1)


class TestMixedIntIndex(Base):
# Mostly the tests from common.py for which the results differ
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,12 @@ def test_cached_data(self):
91 in idx
assert idx._cached_data is None

idx.contains(90)
with tm.assert_produces_warning(FutureWarning):
idx.contains(90)
assert idx._cached_data is None

idx.contains(91)
with tm.assert_produces_warning(FutureWarning):
idx.contains(91)
assert idx._cached_data is None

idx.all()
Expand Down