Skip to content
Open
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
22 changes: 22 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
datetime,
time,
timedelta,
timezone,
)
import re

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected_time = Timestamp(_time)

expected = DataFrame(
{
"id": [1, 2, 3],
"time": [pd.NaT, expected_time, expected_time],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"time": [pd.NaT, expected_time, expected_time],
"time": [pd.NaT, _time, _time],

The issue is about the Python datetime type - we don't want to wrap it with Timestamp

}
)
expected["time"] = expected["time"].astype("datetime64[us, UTC]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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])
Expand Down
Loading