Skip to content

Commit

Permalink
BUG: Fix keyerror bug when indexing multiindex columns with NaT values (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
snitish authored Dec 3, 2024
1 parent 40131a6 commit a6f721e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit a6f721e

Please sign in to comment.