Skip to content

Conversation

@Roxicaro
Copy link
Contributor

@Roxicaro Roxicaro commented Oct 24, 2025

This PR fixes an issue in MultiIndex where keys of type np.datetime64 would cause a silent error that could result in partial/incorrect lookups when compared to levels containing datetime.datetime.

import pandas as pd import numpy as np import datetime as dt dates = [dt.datetime(2023, 11, 1).date(), dt.datetime(2023, 11, 1).date(), dt.datetime(2023, 11, 2).date()] t1 = ["A", "B", "C"] t2 = ["C", "D", "E"] vals = [10, 20, 30] df = pd.DataFrame(data=np.array([dates, t1, t2, vals]).T, columns=["dates", "t1", "t2", "vals"]) df.set_index(["dates", "t1", "t2"], inplace=True) date_np = np.datetime64("2023-11-01") date_dt = dt.datetime(2023, 11, 1).date() #BEFORE THIS FIX print(df.loc[(date_np, "B")]) '''  vals t2  C 10 ''' #AFTER THIS FIX print(df.loc[(date_np, "B")]) '''  vals t2  C 10 D 20 '''

-Added a type-check in _get_loc_level to allow compatible datetime/date types.
-Introduced a helper method _is_key_type_compatible.
-Added tests in pandas/tests/indexes/multi/test_datetime_indexing.py to cover:
--datetime.date vs datetime.datetime
--numpy.datetime64 lookups

Issue Reference

#55969

@Roxicaro Roxicaro force-pushed the fix-multiindex-type-check branch from 40a7d64 to c285c3f Compare October 24, 2025 17:11
@Roxicaro Roxicaro changed the title BUG: Fix MultiIndex key type check for datetime/date (GH#55969) BUG: Fix MultiIndex key type check for datetime/date (#55969) Oct 24, 2025
@Roxicaro Roxicaro changed the title BUG: Fix MultiIndex key type check for datetime/date (#55969) BUG: Fix MultiIndex key type check for datetime/date (Issue #55969) Oct 24, 2025
@Roxicaro Roxicaro marked this pull request as draft October 24, 2025 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants