Skip to content

Commit f828dbc

Browse files
jbrockmendelJulianWgs
authored andcommitted
DEPR: Index.is_type_compatible (pandas-dev#42113)
1 parent 270b510 commit f828dbc

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Other API changes
9696

9797
Deprecations
9898
~~~~~~~~~~~~
99-
-
99+
- Deprecated :meth:`Index.is_type_compatible` (:issue:`42113`)
100100
-
101101

102102
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

+6
Original file line numberDiff line numberDiff line change
@@ -4489,6 +4489,12 @@ def is_type_compatible(self, kind: str_t) -> bool:
44894489
"""
44904490
Whether the index type is compatible with the provided type.
44914491
"""
4492+
warnings.warn(
4493+
"Index.is_type_compatible is deprecated and will be removed in a "
4494+
"future version",
4495+
FutureWarning,
4496+
stacklevel=2,
4497+
)
44924498
return kind == self.inferred_type
44934499

44944500
def __contains__(self, key: Any) -> bool:

pandas/core/indexes/datetimelike.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TypeVar,
1212
cast,
1313
)
14+
import warnings
1415

1516
import numpy as np
1617

@@ -634,6 +635,12 @@ def _has_complex_internals(self) -> bool:
634635
return False
635636

636637
def is_type_compatible(self, kind: str) -> bool:
638+
warnings.warn(
639+
f"{type(self).__name__}.is_type_compatible is deprecated and will be "
640+
"removed in a future version",
641+
FutureWarning,
642+
stacklevel=2,
643+
)
637644
return kind in self._data._infer_matches
638645

639646
# --------------------------------------------------------------------

pandas/core/indexes/multi.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -2792,15 +2792,19 @@ def _partial_tup_index(self, tup: tuple, side="left"):
27922792
n = len(tup)
27932793
start, end = 0, len(self)
27942794
zipped = zip(tup, self.levels, self.codes)
2795-
for k, (lab, lev, labs) in enumerate(zipped):
2796-
section = labs[start:end]
2795+
for k, (lab, lev, level_codes) in enumerate(zipped):
2796+
section = level_codes[start:end]
27972797

27982798
if lab not in lev and not isna(lab):
2799-
if not lev.is_type_compatible(lib.infer_dtype([lab], skipna=False)):
2800-
raise TypeError(f"Level type mismatch: {lab}")
2801-
28022799
# short circuit
2803-
loc = lev.searchsorted(lab, side=side)
2800+
try:
2801+
loc = lev.searchsorted(lab, side=side)
2802+
except TypeError as err:
2803+
# non-comparable e.g. test_slice_locs_with_type_mismatch
2804+
raise TypeError(f"Level type mismatch: {lab}") from err
2805+
if not is_integer(loc):
2806+
# non-comparable level, e.g. test_groupby_example
2807+
raise TypeError(f"Level type mismatch: {lab}")
28042808
if side == "right" and loc >= 0:
28052809
loc -= 1
28062810
return start + section.searchsorted(loc, side=side)

pandas/tests/indexes/test_any_index.py

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def test_ravel_deprecation(index):
6666
index.ravel()
6767

6868

69+
def test_is_type_compatible_deprecation(index):
70+
# GH#42113
71+
msg = "is_type_compatible is deprecated"
72+
with tm.assert_produces_warning(FutureWarning, match=msg):
73+
index.is_type_compatible(index.inferred_type)
74+
75+
6976
class TestConversion:
7077
def test_to_series(self, index):
7178
# assert that we are creating a copy of the index

0 commit comments

Comments
 (0)