REF: Add comprehensive tests for Series indexing and slicing behavior #63345
+504
−221
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tests\series\test_indexing.pyfile if fixing a bug or adding a new feature.AGENTS.md.When performing partial slicing on a three-level MultiIndex where:
The first level (L1) has values of type datetime.date (from the standard library).
The lookup key for L1 is of type np.datetime64.
The lookup key tuple only includes L1 and L2 (e.g., df.loc[(key_L1, key_L2)]).
Pandas incorrectly returns data matching only the L1 key, effectively ignoring the L2 key. This partial slicing failure occurs because the lookup logic seems to fail the key match comparison due to the type mismatch, causing it to fall back to a less specific search across the MultiIndex.
The bug is absent if the lookup explicitly includes slice(None) for the third level (e.g., df.loc[(date, "A", slice(None))]).
Reproducible Example (Failing Code):-
'''
import pandas as pd
import numpy as np
import datetime as dt
Index created with standard library datetime.date objects
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 = np.random.uniform(size=len(dates))
df = pd.DataFrame(data=np.array([dates, t1, t2, vals]).T, columns=["dates", "t1", "t2", "vals"])
df.set_index(["dates", "t1", "t2"], inplace=True)
Lookup key is np.datetime64
date = np.datetime64("2023-11-01")
INCORRECT BEHAVIOR:
df.loc[(date, "A")] - Expected 1 row, but returns 2 rows (matching date, ignoring 'A')
df.loc[(date, "C")] - Expected KeyError, but returns 1 row (matching date, ignoring 'C')
'''
Expected Behavior
The expected behavior should be one of the following:
Strict Type Check: Raise a TypeError or KeyError due to the strict mismatch between the datetime.date index and the np.datetime64 key, forcing the user to cast keys explicitly.
Correct Coercion/Match: Coerce the np.datetime64 key to the index type (datetime.date) and correctly match only the row(s) that match both L1 (date) and L2 ('A' or 'B').
This PR implements the necessary fix to ensure the correct behavior (Option 2) when performing partial slicing with this specific type mismatch.