Skip to content

Commit f449fd4

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 #20511.
1 parent f469af7 commit f449fd4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pandas/tests/indexing/test_iloc.py

+38
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
Interval,
2222
NaT,
2323
Series,
24+
Timestamp,
2425
array,
2526
concat,
2627
date_range,
2728
interval_range,
2829
isna,
30+
to_datetime,
2931
)
3032
import pandas._testing as tm
3133
from pandas.api.types import is_scalar
@@ -1188,6 +1190,24 @@ def test_iloc_getitem_int_single_ea_block_view(self):
11881190
arr[2] = arr[-1]
11891191
assert ser[0] == arr[-1]
11901192

1193+
def test_iloc_setitem_multicolumn_to_datetime(self):
1194+
1195+
# GH#20511
1196+
df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]})
1197+
1198+
df.iloc[:, [0]] = DataFrame({"A": to_datetime(["2021", "2022"])})
1199+
expected = DataFrame(
1200+
{
1201+
"A": [
1202+
Timestamp("2021-01-01 00:00:00"),
1203+
Timestamp("2022-01-01 00:00:00"),
1204+
],
1205+
"B": ["2021", "2022"],
1206+
},
1207+
dtype=object,
1208+
)
1209+
tm.assert_frame_equal(df, expected)
1210+
11911211

11921212
class TestILocErrors:
11931213
# NB: this test should work for _any_ Series we can pass as
@@ -1363,6 +1383,24 @@ def test_frame_iloc_setitem_callable(self):
13631383
exp.iloc[[1, 3], [0]] = [-5, -5]
13641384
tm.assert_frame_equal(res, exp)
13651385

1386+
def test_frame_iloc_setitem_callable_multicolumn_to_datetime(self):
1387+
1388+
# GH#20511
1389+
df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]})
1390+
1391+
df.iloc[:, [0]] = df.iloc[:, [0]].apply(to_datetime)
1392+
expected = DataFrame(
1393+
{
1394+
"A": [
1395+
Timestamp("2022-01-01 00:00:00"),
1396+
Timestamp("2022-01-02 00:00:00"),
1397+
],
1398+
"B": ["2021", "2022"],
1399+
},
1400+
dtype=object,
1401+
)
1402+
tm.assert_frame_equal(df, expected)
1403+
13661404

13671405
class TestILocSeries:
13681406
def test_iloc(self):

0 commit comments

Comments
 (0)