From 0e6a22a0d181ee82a703a52f534fa587ea7c62cf Mon Sep 17 00:00:00 2001 From: Parthi Date: Tue, 10 May 2022 11:32:12 +0530 Subject: [PATCH 1/3] 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. --- pandas/tests/indexing/test_iloc.py | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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): From 6928918dd98c755db889cfbab7de0b58a5d1014c Mon Sep 17 00:00:00 2001 From: Parthi Date: Wed, 11 May 2022 07:57:57 +0530 Subject: [PATCH 2/3] used ArrayManager for type evualation --- pandas/tests/indexing/test_iloc.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index a0509b889e036..db16192af9026 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -1190,7 +1190,7 @@ 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): + def test_iloc_setitem_multicolumn_to_datetime(self, using_array_manager): # GH#20511 df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]}) @@ -1203,10 +1203,9 @@ def test_iloc_setitem_multicolumn_to_datetime(self): Timestamp("2022-01-01 00:00:00"), ], "B": ["2021", "2022"], - }, - dtype=object, + } ) - tm.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected, check_dtype=using_array_manager) class TestILocErrors: @@ -1383,7 +1382,9 @@ 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): + def test_frame_iloc_setitem_callable_multicolumn_to_datetime( + self, using_array_manager + ): # GH#20511 df = DataFrame({"A": ["2022-01-01", "2022-01-02"], "B": ["2021", "2022"]}) @@ -1396,10 +1397,9 @@ def test_frame_iloc_setitem_callable_multicolumn_to_datetime(self): Timestamp("2022-01-02 00:00:00"), ], "B": ["2021", "2022"], - }, - dtype=object, + } ) - tm.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected, check_dtype=using_array_manager) class TestILocSeries: From f7e790265cefdfb9cf28e2570accaf00e252dba8 Mon Sep 17 00:00:00 2001 From: Parthi Date: Thu, 12 May 2022 10:35:23 +0530 Subject: [PATCH 3/3] Removed test --- pandas/tests/indexing/test_iloc.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index db16192af9026..580a2d36cb1c5 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -1382,25 +1382,6 @@ 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, using_array_manager - ): - - # 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"], - } - ) - tm.assert_frame_equal(df, expected, check_dtype=using_array_manager) - class TestILocSeries: def test_iloc(self):