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

DEPR: Index.is_type_compatible #42113

Merged
merged 2 commits into from
Jun 21, 2021
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Other API changes

Deprecations
~~~~~~~~~~~~
-
- Deprecated :meth:`Index.is_type_compatible` (:issue:`42113`)
-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4489,6 +4489,12 @@ def is_type_compatible(self, kind: str_t) -> bool:
"""
Whether the index type is compatible with the provided type.
"""
warnings.warn(
"Index.is_type_compatible is deprecated and will be removed in a "
"future version",
FutureWarning,
stacklevel=2,
)
return kind == self.inferred_type

def __contains__(self, key: Any) -> bool:
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TypeVar,
cast,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -634,6 +635,12 @@ def _has_complex_internals(self) -> bool:
return False

def is_type_compatible(self, kind: str) -> bool:
warnings.warn(
f"{type(self).__name__}.is_type_compatible is deprecated and will be "
"removed in a future version",
FutureWarning,
stacklevel=2,
)
return kind in self._data._infer_matches

# --------------------------------------------------------------------
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,15 +2792,19 @@ def _partial_tup_index(self, tup: tuple, side="left"):
n = len(tup)
start, end = 0, len(self)
zipped = zip(tup, self.levels, self.codes)
for k, (lab, lev, labs) in enumerate(zipped):
section = labs[start:end]
for k, (lab, lev, level_codes) in enumerate(zipped):
section = level_codes[start:end]

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}")

# short circuit
loc = lev.searchsorted(lab, side=side)
try:
loc = lev.searchsorted(lab, side=side)
except TypeError as err:
# non-comparable e.g. test_slice_locs_with_type_mismatch
raise TypeError(f"Level type mismatch: {lab}") from err
if not is_integer(loc):
# non-comparable level, e.g. test_groupby_example
raise TypeError(f"Level type mismatch: {lab}")
if side == "right" and loc >= 0:
loc -= 1
return start + section.searchsorted(loc, side=side)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def test_ravel_deprecation(index):
index.ravel()


def test_is_type_compatible_deprecation(index):
# GH#42113
msg = "is_type_compatible is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
index.is_type_compatible(index.inferred_type)


class TestConversion:
def test_to_series(self, index):
# assert that we are creating a copy of the index
Expand Down