Skip to content

Commit eebc0c8

Browse files
committed
TST: Additional to iloc setitem
This tests make sure when converting multiple columns to datetimes and when assiging back it remains as datetime not as unix date as mentioned in GH pandas-dev#20511.
1 parent 5414a2d commit eebc0c8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/tests/indexing/test_iloc.py

+23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
date_range,
2727
interval_range,
2828
isna,
29+
to_datetime,
2930
)
3031
import pandas._testing as tm
3132
from pandas.api.types import is_scalar
@@ -1188,6 +1189,17 @@ def test_iloc_getitem_int_single_ea_block_view(self):
11881189
arr[2] = arr[-1]
11891190
assert ser[0] == arr[-1]
11901191

1192+
def test_iloc_setitem_multicolumn_to_datetime(self):
1193+
1194+
# GH#20511
1195+
df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]})
1196+
1197+
df.iloc[:, [0]] = DataFrame({"A": to_datetime(["2021", "2022"])})
1198+
expected = DataFrame(
1199+
{"A": to_datetime(["2021", "2022"]), "B": ["2021", "2022"]}
1200+
)
1201+
tm.assert_frame_equal(df, expected, check_dtype=False)
1202+
11911203

11921204
class TestILocErrors:
11931205
# NB: this test should work for _any_ Series we can pass as
@@ -1363,6 +1375,17 @@ def test_frame_iloc_setitem_callable(self):
13631375
exp.iloc[[1, 3], [0]] = [-5, -5]
13641376
tm.assert_frame_equal(res, exp)
13651377

1378+
def test_frame_iloc_setitem_callable_multicolumn_to_datetime(self):
1379+
1380+
# GH#20511
1381+
df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]})
1382+
1383+
df.iloc[:, [0]] = df.iloc[:, [0]].apply(to_datetime)
1384+
expected = DataFrame(
1385+
{"A": to_datetime(["2022-01-01", "2022-01-02"]), "B": ["2021", "2022"]}
1386+
)
1387+
tm.assert_frame_equal(df, expected, check_dtype=False)
1388+
13661389

13671390
class TestILocSeries:
13681391
def test_iloc(self):

0 commit comments

Comments
 (0)