Skip to content

Commit a6f721e

Browse files
authored
BUG: Fix keyerror bug when indexing multiindex columns with NaT values (#60463)
* BUG: Fix keyerror bug when indexing multiindex columns with NaT values * BUG: Update whatsnew/v3.0.0.rst * BUG: Move new test to test_multilevel.py
1 parent 40131a6 commit a6f721e

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ Indexing
667667
^^^^^^^^
668668
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
669669
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
670+
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
670671
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
671672

672673
Missing

pandas/core/indexes/multi.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4084,11 +4084,10 @@ def insert(self, loc: int, item) -> MultiIndex:
40844084
# have to insert into level
40854085
# must insert at end otherwise you have to recompute all the
40864086
# other codes
4087-
if isna(k): # GH 59003
4087+
lev_loc = len(level)
4088+
level = level.insert(lev_loc, k)
4089+
if isna(level[lev_loc]): # GH 59003, 60388
40884090
lev_loc = -1
4089-
else:
4090-
lev_loc = len(level)
4091-
level = level.insert(lev_loc, k)
40924091
else:
40934092
lev_loc = level.get_loc(k)
40944093

pandas/tests/test_multilevel.py

+23
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,29 @@ def test_multiindex_insert_level_with_na(self, na):
295295
df[na, "B"] = 1
296296
tm.assert_frame_equal(df[na], DataFrame([1], columns=["B"]))
297297

298+
def test_multiindex_dt_with_nan(self):
299+
# GH#60388
300+
df = DataFrame(
301+
[
302+
[1, np.nan, 5, np.nan],
303+
[2, np.nan, 6, np.nan],
304+
[np.nan, 3, np.nan, 7],
305+
[np.nan, 4, np.nan, 8],
306+
],
307+
index=Series(["a", "b", "c", "d"], dtype=object, name="sub"),
308+
columns=MultiIndex.from_product(
309+
[
310+
["value1", "value2"],
311+
[datetime.datetime(2024, 11, 1), datetime.datetime(2024, 11, 2)],
312+
],
313+
names=[None, "Date"],
314+
),
315+
)
316+
df = df.reset_index()
317+
result = df[df.columns[0]]
318+
expected = Series(["a", "b", "c", "d"], name=("sub", np.nan))
319+
tm.assert_series_equal(result, expected)
320+
298321

299322
class TestSorted:
300323
"""everything you wanted to test about sorting"""

0 commit comments

Comments
 (0)