Skip to content

BUG: Fix keyerror bug when indexing multiindex columns with NaT values #60463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)

Missing
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4084,11 +4084,10 @@ def insert(self, loc: int, item) -> MultiIndex:
# have to insert into level
# must insert at end otherwise you have to recompute all the
# other codes
if isna(k): # GH 59003
lev_loc = len(level)
level = level.insert(lev_loc, k)
if isna(level[lev_loc]): # GH 59003, 60388
lev_loc = -1
else:
lev_loc = len(level)
level = level.insert(lev_loc, k)
else:
lev_loc = level.get_loc(k)

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,29 @@ def test_multiindex_insert_level_with_na(self, na):
df[na, "B"] = 1
tm.assert_frame_equal(df[na], DataFrame([1], columns=["B"]))

def test_multiindex_dt_with_nan(self):
# GH#60388
df = DataFrame(
[
[1, np.nan, 5, np.nan],
[2, np.nan, 6, np.nan],
[np.nan, 3, np.nan, 7],
[np.nan, 4, np.nan, 8],
],
index=Series(["a", "b", "c", "d"], dtype=object, name="sub"),
columns=MultiIndex.from_product(
[
["value1", "value2"],
[datetime.datetime(2024, 11, 1), datetime.datetime(2024, 11, 2)],
],
names=[None, "Date"],
),
)
df = df.reset_index()
result = df[df.columns[0]]
expected = Series(["a", "b", "c", "d"], name=("sub", np.nan))
tm.assert_series_equal(result, expected)


class TestSorted:
"""everything you wanted to test about sorting"""
Expand Down
Loading