Skip to content

Commit 1eccbd4

Browse files
rohitsanjproost
authored andcommitted
TST: added test for df.loc modify datetime columns (pandas-dev#28964)
* TST: added test for df.loc modifies datetime columns Issue number pandas-dev#28837 * ran black pandas command * MAINT: Address reviewer comments
1 parent 12e4c9a commit 1eccbd4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/tests/indexing/test_loc.py

+26
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,32 @@ def test_loc_assign_non_ns_datetime(self, unit):
712712
expected = Series(df.loc[:, "expected"], name=unit)
713713
tm.assert_series_equal(df.loc[:, unit], expected)
714714

715+
def test_loc_modify_datetime(self):
716+
# see gh-28837
717+
df = DataFrame.from_dict(
718+
{"date": [1485264372711, 1485265925110, 1540215845888, 1540282121025]}
719+
)
720+
721+
df["date_dt"] = pd.to_datetime(df["date"], unit="ms", cache=True)
722+
723+
df.loc[:, "date_dt_cp"] = df.loc[:, "date_dt"]
724+
df.loc[[2, 3], "date_dt_cp"] = df.loc[[2, 3], "date_dt"]
725+
726+
expected = DataFrame(
727+
[
728+
[1485264372711, "2017-01-24 13:26:12.711", "2017-01-24 13:26:12.711"],
729+
[1485265925110, "2017-01-24 13:52:05.110", "2017-01-24 13:52:05.110"],
730+
[1540215845888, "2018-10-22 13:44:05.888", "2018-10-22 13:44:05.888"],
731+
[1540282121025, "2018-10-23 08:08:41.025", "2018-10-23 08:08:41.025"],
732+
],
733+
columns=["date", "date_dt", "date_dt_cp"],
734+
)
735+
736+
columns = ["date_dt", "date_dt_cp"]
737+
expected[columns] = expected[columns].apply(pd.to_datetime)
738+
739+
tm.assert_frame_equal(df, expected)
740+
715741
def test_loc_setitem_frame(self):
716742
df = self.frame_labels
717743

0 commit comments

Comments
 (0)