|
26 | 26 | date_range,
|
27 | 27 | interval_range,
|
28 | 28 | isna,
|
| 29 | + to_datetime, |
29 | 30 | )
|
30 | 31 | import pandas._testing as tm
|
31 | 32 | from pandas.api.types import is_scalar
|
@@ -1188,6 +1189,17 @@ def test_iloc_getitem_int_single_ea_block_view(self):
|
1188 | 1189 | arr[2] = arr[-1]
|
1189 | 1190 | assert ser[0] == arr[-1]
|
1190 | 1191 |
|
| 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 | + |
1191 | 1203 |
|
1192 | 1204 | class TestILocErrors:
|
1193 | 1205 | # NB: this test should work for _any_ Series we can pass as
|
@@ -1363,6 +1375,17 @@ def test_frame_iloc_setitem_callable(self):
|
1363 | 1375 | exp.iloc[[1, 3], [0]] = [-5, -5]
|
1364 | 1376 | tm.assert_frame_equal(res, exp)
|
1365 | 1377 |
|
| 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 | + |
1366 | 1389 |
|
1367 | 1390 | class TestILocSeries:
|
1368 | 1391 | def test_iloc(self):
|
|
0 commit comments