From 221de5685f286a3902f607e53005f01b76c20966 Mon Sep 17 00:00:00 2001 From: AlonMenczer Date: Sat, 3 Dec 2022 13:58:44 +0200 Subject: [PATCH 1/5] test: Add test for loc assignment changes datetime dtype --- pandas/tests/indexing/test_loc.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 11cbcfe231928..42d60867e3343 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -5,6 +5,7 @@ datetime, time, timedelta, + timezone, ) import re @@ -2971,6 +2972,27 @@ def test_loc_periodindex_3_levels(): assert mi_series.loc[(p_index[0], "A", "B")] == 1.0 +@pytest.mark.parametrize("utc", [False, True]) +def test_loc_datetime_assignment_dtype_does_not_change(utc): + # GH#49837 + df = pd.DataFrame( + { + # Checking with and without UTC as the original bug didn't occur with utc=True + "date": pd.to_datetime( + [datetime(2022, 1, 20), datetime(2022, 1, 22)], utc=utc + ), + "update": [True, False], + } + ) + # We don't expect the dataframe to be changes at all + expected = df.copy(deep=True) + + update_df = df[df["update"]] + df.loc[df["update"], ["date"]] = update_df["date"]) + + tm.assert_frame_equal(df, expected) + + class TestLocSeries: @pytest.mark.parametrize("val,expected", [(2**63 - 1, 3), (2**63, 4)]) def test_loc_uint64(self, val, expected): From 597b516ed8b734573c1e25559a5176649c8f5be5 Mon Sep 17 00:00:00 2001 From: alm Date: Sat, 3 Dec 2022 14:00:19 +0200 Subject: [PATCH 2/5] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 42d60867e3343..222ab104bab33 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -5,7 +5,6 @@ datetime, time, timedelta, - timezone, ) import re From 997f50d601f7aea7f8d2cf18ce12eb1d5c8e3f79 Mon Sep 17 00:00:00 2001 From: AlonMenczer Date: Sat, 3 Dec 2022 14:21:08 +0200 Subject: [PATCH 3/5] fix pre-commit --- pandas/tests/indexing/test_loc.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 222ab104bab33..e3a9526012199 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2974,10 +2974,11 @@ def test_loc_periodindex_3_levels(): @pytest.mark.parametrize("utc", [False, True]) def test_loc_datetime_assignment_dtype_does_not_change(utc): # GH#49837 - df = pd.DataFrame( + df = DataFrame( { - # Checking with and without UTC as the original bug didn't occur with utc=True - "date": pd.to_datetime( + # Checking with and without UTC as the original bug didn't occur + # for utc=True + "date": to_datetime( [datetime(2022, 1, 20), datetime(2022, 1, 22)], utc=utc ), "update": [True, False], @@ -2987,7 +2988,7 @@ def test_loc_datetime_assignment_dtype_does_not_change(utc): expected = df.copy(deep=True) update_df = df[df["update"]] - df.loc[df["update"], ["date"]] = update_df["date"]) + df.loc[df["update"], ["date"]] = update_df["date"] tm.assert_frame_equal(df, expected) From ba9b41d79833920dba1a46ffd482a57bf842622f Mon Sep 17 00:00:00 2001 From: AlonMenczer Date: Sat, 3 Dec 2022 21:04:24 +0200 Subject: [PATCH 4/5] Remove comment, add test case, move file --- pandas/tests/frame/indexing/test_indexing.py | 27 ++++++++++++++++++++ pandas/tests/indexing/test_loc.py | 22 ---------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index be67ce50a0634..53e2588d72332 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -29,6 +29,7 @@ date_range, isna, notna, + to_datetime, ) import pandas._testing as tm @@ -1454,6 +1455,32 @@ def test_loc_bool_multiindex(self, dtype, indexer): ) tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("utc", [False, True]) + @pytest.mark.parametrize("column_label_array", [False, True]) + def test_loc_datetime_assignment_dtype_does_not_change( + self, utc, column_label_array + ): + # GH#49837 + df = DataFrame( + { + "date": to_datetime( + [datetime(2022, 1, 20), datetime(2022, 1, 22)], utc=utc + ), + "update": [True, False], + } + ) + expected = df.copy(deep=True) + + update_df = df[df["update"]] + + column = "date" + if column_label_array: + column = [column] + + df.loc[df["update"], column] = update_df["date"] + + tm.assert_frame_equal(df, expected) + class TestDataFrameIndexingUInt64: def test_setitem(self, uint64_frame): diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index e3a9526012199..11cbcfe231928 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2971,28 +2971,6 @@ def test_loc_periodindex_3_levels(): assert mi_series.loc[(p_index[0], "A", "B")] == 1.0 -@pytest.mark.parametrize("utc", [False, True]) -def test_loc_datetime_assignment_dtype_does_not_change(utc): - # GH#49837 - df = DataFrame( - { - # Checking with and without UTC as the original bug didn't occur - # for utc=True - "date": to_datetime( - [datetime(2022, 1, 20), datetime(2022, 1, 22)], utc=utc - ), - "update": [True, False], - } - ) - # We don't expect the dataframe to be changes at all - expected = df.copy(deep=True) - - update_df = df[df["update"]] - df.loc[df["update"], ["date"]] = update_df["date"] - - tm.assert_frame_equal(df, expected) - - class TestLocSeries: @pytest.mark.parametrize("val,expected", [(2**63 - 1, 3), (2**63, 4)]) def test_loc_uint64(self, val, expected): From 5efe4c74a0131e02baf1ff176a5fea7b98d05775 Mon Sep 17 00:00:00 2001 From: AlonMenczer Date: Sun, 4 Dec 2022 09:18:18 +0200 Subject: [PATCH 5/5] Simplify params --- pandas/tests/frame/indexing/test_indexing.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 53e2588d72332..81a5e3d9947be 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1456,10 +1456,8 @@ def test_loc_bool_multiindex(self, dtype, indexer): tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("utc", [False, True]) - @pytest.mark.parametrize("column_label_array", [False, True]) - def test_loc_datetime_assignment_dtype_does_not_change( - self, utc, column_label_array - ): + @pytest.mark.parametrize("indexer", ["date", ["date"]]) + def test_loc_datetime_assignment_dtype_does_not_change(self, utc, indexer): # GH#49837 df = DataFrame( { @@ -1473,11 +1471,7 @@ def test_loc_datetime_assignment_dtype_does_not_change( update_df = df[df["update"]] - column = "date" - if column_label_array: - column = [column] - - df.loc[df["update"], column] = update_df["date"] + df.loc[df["update"], indexer] = update_df["date"] tm.assert_frame_equal(df, expected)