Skip to content

Commit 1ffa939

Browse files
committed
TST: add regression test for GH#54409 (tz-aware NaT indexing with loc)
1 parent 1feacde commit 1ffa939

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/indexing/test_loc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,31 @@ def test_loc_setitem_numpy_frame_categorical_value(self):
16291629
expected = DataFrame({"a": [1, 2, 2, 1, 1], "b": ["a", "a", "a", "a", "a"]})
16301630
tm.assert_frame_equal(df, expected)
16311631

1632+
def test_loc_with_nat_in_tzaware_index(self):
1633+
# GH#54409
1634+
timestamp = to_datetime("2023-01-01", utc=True)
1635+
df = DataFrame(
1636+
{
1637+
"index": Series([pd.NaT, timestamp]),
1638+
"value": Series([0, 1]),
1639+
}
1640+
).set_index("index")
1641+
1642+
# Works fine when mixing NaT and valid values
1643+
result = df.loc[
1644+
Series([pd.NaT, timestamp, timestamp], dtype=df.index.dtype),
1645+
"value",
1646+
]
1647+
expected = [0, 1, 1]
1648+
assert result.tolist() == expected
1649+
1650+
# Regression check: all-NaT lookup should return [0], not raise
1651+
result = df.loc[
1652+
Series([pd.NaT], dtype=df.index.dtype),
1653+
"value",
1654+
]
1655+
assert result.tolist() == [0]
1656+
16321657

16331658
class TestLocWithEllipsis:
16341659
@pytest.fixture

0 commit comments

Comments
 (0)