Skip to content

Commit c285c3f

Browse files
committed
BUG: refactor _is_key_type_compatible to contemplate CI tests (GH#55969)
1 parent c44d236 commit c285c3f

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pandas/core/indexes/multi.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
lib,
3232
)
3333
from pandas._libs.hashtable import duplicated
34-
from pandas._libs.tslibs.timestamps import Timestamp
3534
from pandas._typing import (
3635
AnyAll,
3736
AnyArrayLike,
@@ -3612,34 +3611,43 @@ def maybe_mi_droplevels(indexer, levels):
36123611

36133612
def _is_key_type_compatible(self, key, level):
36143613
"""
3615-
Return True if the key is compatible with the type of the level's values.
3614+
Return True if the key type is compatible with the type of the level's values.
3615+
3616+
Compatible types:
3617+
- int ↔ np.integer
3618+
- float ↔ np.floating
3619+
- str ↔ np.str_
3620+
- datetime.date ↔ datetime.datetime
3621+
- slices (for partial indexing)
36163622
"""
36173623
if len(self.levels[level]) == 0:
36183624
return True # nothing to compare
36193625

3620-
level_type = self.levels[level][0]
3626+
level_val = self.levels[level][0]
3627+
level_type = type(level_val)
36213628

3622-
# Allow slices (used in partial indexing)
3623-
if isinstance(key, slice):
3629+
# Same type
3630+
if isinstance(key, level_type):
36243631
return True
36253632

3626-
# datetime/date/Timestamp compatibility
3627-
datetime_types = (datetime.date, np.datetime64, Timestamp)
3628-
if isinstance(level_type, datetime_types) and isinstance(
3629-
key, datetime_types + (str,)
3630-
):
3633+
# NumPy integer / float / string compatibility
3634+
if isinstance(level_val, np.integer) and isinstance(key, int):
3635+
return True
3636+
if isinstance(level_val, np.floating) and isinstance(key, float):
3637+
return True
3638+
if isinstance(level_val, np.str_) and isinstance(key, str):
36313639
return True
36323640

3633-
# numeric compatibility
3634-
if np.issubdtype(type(level_type), np.integer) and isinstance(key, int):
3641+
# Allow subclasses of datetime.date for datetime levels
3642+
if isinstance(level_val, datetime.date) and isinstance(key, datetime.date):
36353643
return True
3636-
if np.issubdtype(type(level_type), np.floating) and isinstance(
3637-
key, (int, float)
3638-
):
3644+
3645+
# Allow slices (used internally for partial selection)
3646+
if isinstance(key, slice):
36393647
return True
36403648

3641-
# string compatibility
3642-
if isinstance(level_type, str) and isinstance(key, str):
3649+
# Allow any NumPy generic types for flexibility
3650+
if isinstance(key, np.generic):
36433651
return True
36443652

36453653
return False

0 commit comments

Comments
 (0)