From 9ee4039f68512861612ae29b6644a1a33a937514 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 21 Jun 2021 07:26:43 -0700 Subject: [PATCH] DEPR: Index.is_type_compatible (#42113) --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/core/indexes/base.py | 6 ++++++ pandas/core/indexes/datetimelike.py | 7 +++++++ pandas/core/indexes/multi.py | 16 ++++++++++------ pandas/tests/indexes/test_any_index.py | 7 +++++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 9859f12a34621..f992d6aa09ead 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -96,7 +96,7 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- +- Deprecated :meth:`Index.is_type_compatible` (:issue:`42113`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 88fded29810ff..b092bd6666fc0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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: diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 7143b275214ee..417d517081863 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -11,6 +11,7 @@ TypeVar, cast, ) +import warnings import numpy as np @@ -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 # -------------------------------------------------------------------- diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index f1f418453c68b..7b70a26155358 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -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) diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index 60fa8f1a0c083..f7dcaa628228b 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -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