Skip to content

Commit 221de56

Browse files
committed
test: Add test for loc assignment changes datetime dtype
1 parent a85a386 commit 221de56

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/tests/indexing/test_loc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
datetime,
66
time,
77
timedelta,
8+
timezone,
89
)
910
import re
1011

@@ -2971,6 +2972,27 @@ def test_loc_periodindex_3_levels():
29712972
assert mi_series.loc[(p_index[0], "A", "B")] == 1.0
29722973

29732974

2975+
@pytest.mark.parametrize("utc", [False, True])
2976+
def test_loc_datetime_assignment_dtype_does_not_change(utc):
2977+
# GH#49837
2978+
df = pd.DataFrame(
2979+
{
2980+
# Checking with and without UTC as the original bug didn't occur with utc=True
2981+
"date": pd.to_datetime(
2982+
[datetime(2022, 1, 20), datetime(2022, 1, 22)], utc=utc
2983+
),
2984+
"update": [True, False],
2985+
}
2986+
)
2987+
# We don't expect the dataframe to be changes at all
2988+
expected = df.copy(deep=True)
2989+
2990+
update_df = df[df["update"]]
2991+
df.loc[df["update"], ["date"]] = update_df["date"])
2992+
2993+
tm.assert_frame_equal(df, expected)
2994+
2995+
29742996
class TestLocSeries:
29752997
@pytest.mark.parametrize("val,expected", [(2**63 - 1, 3), (2**63, 4)])
29762998
def test_loc_uint64(self, val, expected):

0 commit comments

Comments
 (0)