diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 426192ab46914..a0509b889e036 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -21,11 +21,13 @@ Interval, NaT, Series, + Timestamp, array, concat, date_range, interval_range, isna, + to_datetime, ) import pandas._testing as tm from pandas.api.types import is_scalar @@ -1188,6 +1190,24 @@ def test_iloc_getitem_int_single_ea_block_view(self): arr[2] = arr[-1] assert ser[0] == arr[-1] + def test_iloc_setitem_multicolumn_to_datetime(self): + + # GH#20511 + df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]}) + + df.iloc[:, [0]] = DataFrame({"A": to_datetime(["2021", "2022"])}) + expected = DataFrame( + { + "A": [ + Timestamp("2021-01-01 00:00:00"), + Timestamp("2022-01-01 00:00:00"), + ], + "B": ["2021", "2022"], + }, + dtype=object, + ) + tm.assert_frame_equal(df, expected) + class TestILocErrors: # NB: this test should work for _any_ Series we can pass as @@ -1363,6 +1383,24 @@ def test_frame_iloc_setitem_callable(self): exp.iloc[[1, 3], [0]] = [-5, -5] tm.assert_frame_equal(res, exp) + def test_frame_iloc_setitem_callable_multicolumn_to_datetime(self): + + # GH#20511 + df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]}) + + df.iloc[:, [0]] = df.iloc[:, [0]].apply(to_datetime) + expected = DataFrame( + { + "A": [ + Timestamp("2022-01-01 00:00:00"), + Timestamp("2022-01-02 00:00:00"), + ], + "B": ["2021", "2022"], + }, + dtype=object, + ) + tm.assert_frame_equal(df, expected) + class TestILocSeries: def test_iloc(self):