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