|
21 | 21 | Interval,
|
22 | 22 | NaT,
|
23 | 23 | Series,
|
| 24 | + Timestamp, |
24 | 25 | array,
|
25 | 26 | concat,
|
26 | 27 | date_range,
|
27 | 28 | interval_range,
|
28 | 29 | isna,
|
| 30 | + to_datetime, |
29 | 31 | )
|
30 | 32 | import pandas._testing as tm
|
31 | 33 | from pandas.api.types import is_scalar
|
@@ -1188,6 +1190,24 @@ def test_iloc_getitem_int_single_ea_block_view(self):
|
1188 | 1190 | arr[2] = arr[-1]
|
1189 | 1191 | assert ser[0] == arr[-1]
|
1190 | 1192 |
|
| 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 | + |
1191 | 1211 |
|
1192 | 1212 | class TestILocErrors:
|
1193 | 1213 | # NB: this test should work for _any_ Series we can pass as
|
@@ -1363,6 +1383,24 @@ def test_frame_iloc_setitem_callable(self):
|
1363 | 1383 | exp.iloc[[1, 3], [0]] = [-5, -5]
|
1364 | 1384 | tm.assert_frame_equal(res, exp)
|
1365 | 1385 |
|
| 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 | + |
1366 | 1404 |
|
1367 | 1405 | class TestILocSeries:
|
1368 | 1406 | def test_iloc(self):
|
|
0 commit comments