From 7d88e10e8581a42ac82d6071e3f868e5416347b9 Mon Sep 17 00:00:00 2001 From: dcamejo1 Date: Wed, 15 Oct 2025 01:54:32 -0700 Subject: [PATCH 1/2] TST: Add test for loc setitem with expansion preserving tz-aware dtype --- pandas/tests/indexing/test_loc.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 8e4845a72ec35..ad5e2edbbe203 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -7,6 +7,7 @@ datetime, time, timedelta, + timezone, ) import re @@ -2702,6 +2703,27 @@ def test_loc_indexer_length_one(self): df.loc[np.array([True], dtype=np.bool_), ["a"]] = df["b"].copy() tm.assert_frame_equal(df, expected) + def test_loc_setitem_bool_mask_tzaware_scalar_with_expansion(self): + # GH#55423 + + df = DataFrame([{"id": 1}, {"id": 2}, {"id": 3}]) + _time = datetime.fromtimestamp(1695887042, tz=timezone.utc) + + df.loc[df.id >= 2, "time"] = _time + + assert isinstance(df["time"].dtype, pd.DatetimeTZDtype) + assert str(df["time"].dtype.tz) == "UTC" + + expected_time = Timestamp(_time) + expected = DataFrame( + { + "id": [1, 2, 3], + "time": [pd.NaT, expected_time, expected_time], + } + ) + expected["time"] = expected["time"].astype("datetime64[us, UTC]") + tm.assert_frame_equal(df, expected) + class TestLocListlike: @pytest.mark.parametrize("box", [lambda x: x, np.asarray, list]) From 996c93011f15759cc6021531873c9e8b023ef938 Mon Sep 17 00:00:00 2001 From: Daniel Camejo <71190106+dcamejo1@users.noreply.github.com> Date: Thu, 30 Oct 2025 22:34:05 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Commit suggestions Co-authored-by: William Ayd --- pandas/tests/indexing/test_loc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index ad5e2edbbe203..36c498971866f 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2714,14 +2714,12 @@ def test_loc_setitem_bool_mask_tzaware_scalar_with_expansion(self): assert isinstance(df["time"].dtype, pd.DatetimeTZDtype) assert str(df["time"].dtype.tz) == "UTC" - expected_time = Timestamp(_time) expected = DataFrame( { "id": [1, 2, 3], - "time": [pd.NaT, expected_time, expected_time], + "time": [pd.NaT, _time, _time], } ) - expected["time"] = expected["time"].astype("datetime64[us, UTC]") tm.assert_frame_equal(df, expected)