From a1f3df430a9d78086fed788ab8e2dd71da59547a Mon Sep 17 00:00:00 2001 From: Mike Phung Date: Mon, 26 Jul 2021 23:29:12 -0700 Subject: [PATCH 1/5] TST GH27994 Add test for dropping empty list / index for a DataFrame with a non-unique datetime index. --- pandas/tests/frame/methods/test_drop.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index b3eeab9db4ad5..cf0d78eb7c43e 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -268,6 +268,30 @@ def test_drop_non_empty_list(self, index, drop_labels): with pytest.raises(KeyError, match="not found in axis"): DataFrame(index=index).drop(drop_labels) + def test_drop_empty_listlike_non_unique_datetime_index(self): + # GH#27994 + data = { + "column_a": [5, 10, 15, 20, 25], + "column_b": ["one", "two", "three", "four", "five"], + } + index = [ + Timestamp("2021-01-01"), + Timestamp("2021-01-01"), + Timestamp("2021-01-01"), + Timestamp("2021-01-01"), + Timestamp("2021-01-01"), + ] + df = DataFrame(data, index=index) + + # Passing empty list or index should return the same DataFrame. + empty_list = [] + empty_datetime_index = df[df["column_a"] > 100].index + + for empty_listlike in [empty_list, empty_datetime_index]: + dropped = df.drop(empty_listlike) + expected = df + tm.assert_frame_equal(dropped, expected) + def test_mixed_depth_drop(self): arrays = [ ["a", "top", "top", "routine1", "routine1", "routine2"], From 1bbd4c0ec10a394a09fe0ccbb58bf06882fa2c92 Mon Sep 17 00:00:00 2001 From: Mike Phung Date: Tue, 27 Jul 2021 08:30:04 -0700 Subject: [PATCH 2/5] TST GH27994 Update new test to reduce rows, safely copy data and use parameters. --- pandas/tests/frame/methods/test_drop.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index cf0d78eb7c43e..4edaeab68c927 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -8,6 +8,7 @@ import pandas as pd from pandas import ( DataFrame, + DatetimeIndex, Index, MultiIndex, Series, @@ -268,29 +269,23 @@ def test_drop_non_empty_list(self, index, drop_labels): with pytest.raises(KeyError, match="not found in axis"): DataFrame(index=index).drop(drop_labels) - def test_drop_empty_listlike_non_unique_datetime_index(self): + @pytest.mark.parametrize("empty_listlike", [[], DatetimeIndex([])]) + def test_drop_empty_listlike_non_unique_datetime_index(self, empty_listlike): # GH#27994 data = { - "column_a": [5, 10, 15, 20, 25], - "column_b": ["one", "two", "three", "four", "five"], + "column_a": [5, 10], + "column_b": ["one", "two"] } index = [ Timestamp("2021-01-01"), - Timestamp("2021-01-01"), - Timestamp("2021-01-01"), - Timestamp("2021-01-01"), - Timestamp("2021-01-01"), + Timestamp("2021-01-01") ] df = DataFrame(data, index=index) - # Passing empty list or index should return the same DataFrame. - empty_list = [] - empty_datetime_index = df[df["column_a"] > 100].index - - for empty_listlike in [empty_list, empty_datetime_index]: - dropped = df.drop(empty_listlike) - expected = df - tm.assert_frame_equal(dropped, expected) + # Passing empty list-like should return the same DataFrame. + expected = df.copy() + dropped = df.drop(empty_listlike) + tm.assert_frame_equal(dropped, expected) def test_mixed_depth_drop(self): arrays = [ From be3bd20a6a37bd9a774be64a6aafcdaf238a481d Mon Sep 17 00:00:00 2001 From: Mike Phung Date: Tue, 27 Jul 2021 08:59:52 -0700 Subject: [PATCH 3/5] TST GH27994 Update new test to reduce line-breaks for index and re-name result. --- pandas/tests/frame/methods/test_drop.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 4edaeab68c927..c97fefc7f5909 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -276,16 +276,13 @@ def test_drop_empty_listlike_non_unique_datetime_index(self, empty_listlike): "column_a": [5, 10], "column_b": ["one", "two"] } - index = [ - Timestamp("2021-01-01"), - Timestamp("2021-01-01") - ] + index = [Timestamp("2021-01-01"), Timestamp("2021-01-01")] df = DataFrame(data, index=index) # Passing empty list-like should return the same DataFrame. expected = df.copy() - dropped = df.drop(empty_listlike) - tm.assert_frame_equal(dropped, expected) + result = df.drop(empty_listlike) + tm.assert_frame_equal(result, expected) def test_mixed_depth_drop(self): arrays = [ From 8682c3675faa0bef2dcc1056ee0ac005a1645516 Mon Sep 17 00:00:00 2001 From: Mike Phung Date: Tue, 27 Jul 2021 09:02:16 -0700 Subject: [PATCH 4/5] TST GH27994 Apply black code formatting. --- pandas/tests/frame/methods/test_drop.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index c97fefc7f5909..6ad5f90fde4c6 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -272,10 +272,7 @@ def test_drop_non_empty_list(self, index, drop_labels): @pytest.mark.parametrize("empty_listlike", [[], DatetimeIndex([])]) def test_drop_empty_listlike_non_unique_datetime_index(self, empty_listlike): # GH#27994 - data = { - "column_a": [5, 10], - "column_b": ["one", "two"] - } + data = {"column_a": [5, 10], "column_b": ["one", "two"]} index = [Timestamp("2021-01-01"), Timestamp("2021-01-01")] df = DataFrame(data, index=index) From 71f01a885f51544b4d9f388162d0476686061175 Mon Sep 17 00:00:00 2001 From: Mike Phung Date: Tue, 27 Jul 2021 12:35:12 -0700 Subject: [PATCH 5/5] TST GH27994 Add additional empty list-like objects to test. --- pandas/tests/frame/methods/test_drop.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 6ad5f90fde4c6..be29a3e50b9fa 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -269,7 +269,17 @@ def test_drop_non_empty_list(self, index, drop_labels): with pytest.raises(KeyError, match="not found in axis"): DataFrame(index=index).drop(drop_labels) - @pytest.mark.parametrize("empty_listlike", [[], DatetimeIndex([])]) + @pytest.mark.parametrize( + "empty_listlike", + [ + [], + {}, + np.array([]), + Series([], dtype="datetime64[ns]"), + Index([]), + DatetimeIndex([]), + ], + ) def test_drop_empty_listlike_non_unique_datetime_index(self, empty_listlike): # GH#27994 data = {"column_a": [5, 10], "column_b": ["one", "two"]}