From 5c0a09558ca361d30563f63a5d204f0825ede699 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Fri, 3 Jul 2020 13:47:17 +0200 Subject: [PATCH 01/11] TST add test case to drop_duplicates for inplace=True --- pandas/tests/frame/methods/test_drop_duplicates.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 7c6391140e2bb..983696587c5e1 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -425,3 +425,8 @@ def test_drop_duplicates_null_in_object_column(nulls_fixture): df = DataFrame([[1, nulls_fixture], [2, "a"]], dtype=object) result = df.drop_duplicates() tm.assert_frame_equal(result, df) + +def test_drop_duplicates_inplace(): + df = DataFrame({"a": [1,2,3], "b": [1,2,4], "c": [1,6,5]}) + result = df.drop_duplicates(inplace=True) + assert result is None From 6b940bb782995e0306f339c2ae797f983e6a9822 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sat, 4 Jul 2020 14:22:54 +0200 Subject: [PATCH 02/11] CLN PEP-8 --- pandas/tests/frame/methods/test_drop_duplicates.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 983696587c5e1..7104f932f8300 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -426,7 +426,8 @@ def test_drop_duplicates_null_in_object_column(nulls_fixture): result = df.drop_duplicates() tm.assert_frame_equal(result, df) -def test_drop_duplicates_inplace(): - df = DataFrame({"a": [1,2,3], "b": [1,2,4], "c": [1,6,5]}) + +def test_drop_duplicates_inplace_result(): + df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 4], "c": [1, 6, 5]}) result = df.drop_duplicates(inplace=True) assert result is None From 4b8679c1b3fc9319f46bcbd6031e76b0d0b73d1e Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Tue, 7 Jul 2020 19:41:56 +0200 Subject: [PATCH 03/11] TST move to existing test --- .../frame/methods/test_drop_duplicates.py | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 7104f932f8300..64fb1ce7ab6b2 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -333,64 +333,73 @@ def test_drop_duplicates_inplace(): ) # single column df = orig.copy() - df.drop_duplicates("A", inplace=True) + return_value = df.drop_duplicates("A", inplace=True) expected = orig[:2] result = df tm.assert_frame_equal(result, expected) + assert(return_value is None) df = orig.copy() - df.drop_duplicates("A", keep="last", inplace=True) + return_value = df.drop_duplicates("A", keep="last", inplace=True) expected = orig.loc[[6, 7]] result = df tm.assert_frame_equal(result, expected) + assert (return_value is None) df = orig.copy() - df.drop_duplicates("A", keep=False, inplace=True) + return_value = df.drop_duplicates("A", keep=False, inplace=True) expected = orig.loc[[]] result = df tm.assert_frame_equal(result, expected) assert len(df) == 0 + assert (return_value is None) # multi column df = orig.copy() - df.drop_duplicates(["A", "B"], inplace=True) + return_value = df.drop_duplicates(["A", "B"], inplace=True) expected = orig.loc[[0, 1, 2, 3]] result = df tm.assert_frame_equal(result, expected) + assert (return_value is None) df = orig.copy() - df.drop_duplicates(["A", "B"], keep="last", inplace=True) + return_value = df.drop_duplicates(["A", "B"], keep="last", inplace=True) expected = orig.loc[[0, 5, 6, 7]] result = df tm.assert_frame_equal(result, expected) + assert (return_value is None) df = orig.copy() - df.drop_duplicates(["A", "B"], keep=False, inplace=True) + return_value = df.drop_duplicates(["A", "B"], keep=False, inplace=True) expected = orig.loc[[0]] result = df tm.assert_frame_equal(result, expected) + assert (return_value is None) # consider everything orig2 = orig.loc[:, ["A", "B", "C"]].copy() df2 = orig2.copy() - df2.drop_duplicates(inplace=True) + return_value = df2.drop_duplicates(inplace=True) # in this case only expected = orig2.drop_duplicates(["A", "B"]) result = df2 tm.assert_frame_equal(result, expected) + assert (return_value is None) df2 = orig2.copy() - df2.drop_duplicates(keep="last", inplace=True) + return_value = df2.drop_duplicates(keep="last", inplace=True) expected = orig2.drop_duplicates(["A", "B"], keep="last") result = df2 tm.assert_frame_equal(result, expected) + assert (return_value is None) df2 = orig2.copy() - df2.drop_duplicates(keep=False, inplace=True) + return_value = df2.drop_duplicates(keep=False, inplace=True) expected = orig2.drop_duplicates(["A", "B"], keep=False) result = df2 tm.assert_frame_equal(result, expected) + assert (return_value is None) @pytest.mark.parametrize("inplace", [True, False]) @@ -425,9 +434,3 @@ def test_drop_duplicates_null_in_object_column(nulls_fixture): df = DataFrame([[1, nulls_fixture], [2, "a"]], dtype=object) result = df.drop_duplicates() tm.assert_frame_equal(result, df) - - -def test_drop_duplicates_inplace_result(): - df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 4], "c": [1, 6, 5]}) - result = df.drop_duplicates(inplace=True) - assert result is None From cb2ab5e3ab2f0b3b755a467b312b610659ec46c8 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Tue, 7 Jul 2020 19:55:19 +0200 Subject: [PATCH 04/11] CLN remove parenthesis --- .../frame/methods/test_drop_duplicates.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 64fb1ce7ab6b2..cebec215a0d9d 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -337,14 +337,14 @@ def test_drop_duplicates_inplace(): expected = orig[:2] result = df tm.assert_frame_equal(result, expected) - assert(return_value is None) + assert return_value is None df = orig.copy() return_value = df.drop_duplicates("A", keep="last", inplace=True) expected = orig.loc[[6, 7]] result = df tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None df = orig.copy() return_value = df.drop_duplicates("A", keep=False, inplace=True) @@ -352,7 +352,7 @@ def test_drop_duplicates_inplace(): result = df tm.assert_frame_equal(result, expected) assert len(df) == 0 - assert (return_value is None) + assert return_value is None # multi column df = orig.copy() @@ -360,21 +360,21 @@ def test_drop_duplicates_inplace(): expected = orig.loc[[0, 1, 2, 3]] result = df tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None df = orig.copy() return_value = df.drop_duplicates(["A", "B"], keep="last", inplace=True) expected = orig.loc[[0, 5, 6, 7]] result = df tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None df = orig.copy() return_value = df.drop_duplicates(["A", "B"], keep=False, inplace=True) expected = orig.loc[[0]] result = df tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None # consider everything orig2 = orig.loc[:, ["A", "B", "C"]].copy() @@ -385,21 +385,21 @@ def test_drop_duplicates_inplace(): expected = orig2.drop_duplicates(["A", "B"]) result = df2 tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None df2 = orig2.copy() return_value = df2.drop_duplicates(keep="last", inplace=True) expected = orig2.drop_duplicates(["A", "B"], keep="last") result = df2 tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None df2 = orig2.copy() return_value = df2.drop_duplicates(keep=False, inplace=True) expected = orig2.drop_duplicates(["A", "B"], keep=False) result = df2 tm.assert_frame_equal(result, expected) - assert (return_value is None) + assert return_value is None @pytest.mark.parametrize("inplace", [True, False]) From 23691419be82bf0dfa1391cbc03716cf1e77de76 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Thu, 9 Jul 2020 11:09:00 +0200 Subject: [PATCH 05/11] TST test from_tuple corner cases --- pandas/tests/frame/test_constructors.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index dba243f1a339a..f90024cccc0a4 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1168,6 +1168,11 @@ def test_constructor_list_of_odicts(self): OrderedDict([["b", 3], ["c", 4], ["d", 6]]), ] + result = DataFrame([data[0]]) + expected = DataFrame.from_dict( + dict(zip([0], [data[0]])), orient="index") + tm.assert_frame_equal(result, expected.reindex(result.index)) + result = DataFrame(data) expected = DataFrame.from_dict( dict(zip(range(len(data)), data)), orient="index" @@ -1493,16 +1498,17 @@ def test_from_dict_columns_parameter(self): ) @pytest.mark.parametrize( - "data_dict, keys", + "data_dict, keys, orient", [ - ([{("a",): 1}, {("a",): 2}], [("a",)]), - ([OrderedDict([(("a",), 1), (("b",), 2)])], [("a",), ("b",)]), - ([{("a", "b"): 1}], [("a", "b")]), + ({}, [], "index"), + ([{("a",): 1}, {("a",): 2}], [("a",)], "columns"), + ([OrderedDict([(("a",), 1), (("b",), 2)])], [("a",), ("b",)], "columns"), + ([{("a", "b"): 1}], [("a", "b")], "columns"), ], ) - def test_constructor_from_dict_tuples(self, data_dict, keys): + def test_constructor_from_dict_tuples(self, data_dict, keys, orient): # GH 16769 - df = DataFrame.from_dict(data_dict) + df = DataFrame.from_dict(data_dict, orient) result = df.columns expected = Index(keys, dtype="object", tupleize_cols=False) From 04a86a794073dd780de14d88a131d5a7b39f161e Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Fri, 10 Jul 2020 18:15:55 +0200 Subject: [PATCH 06/11] add comment --- pandas/tests/frame/test_constructors.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index df2b9f291708f..f8a2f0db37885 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1168,6 +1168,7 @@ def test_constructor_list_of_odicts(self): OrderedDict([["b", 3], ["c", 4], ["d", 6]]), ] + # single row dataframe result = DataFrame([data[0]]) expected = DataFrame.from_dict( dict(zip([0], [data[0]])), orient="index") From c1cf452ae0b79c22e64c82c357090d6cbe83dfb1 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sat, 11 Jul 2020 11:46:18 +0200 Subject: [PATCH 07/11] CLN run black formatting --- pandas/tests/frame/test_constructors.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index f8a2f0db37885..09eef6fc5a8eb 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1170,8 +1170,7 @@ def test_constructor_list_of_odicts(self): # single row dataframe result = DataFrame([data[0]]) - expected = DataFrame.from_dict( - dict(zip([0], [data[0]])), orient="index") + expected = DataFrame.from_dict(dict(zip([0], [data[0]])), orient="index") tm.assert_frame_equal(result, expected.reindex(result.index)) result = DataFrame(data) From 40faa87d916ca7af1e97029ab362a0742158e015 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sat, 11 Jul 2020 11:55:33 +0200 Subject: [PATCH 08/11] CLN refactor case as separate test --- pandas/tests/frame/test_constructors.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 09eef6fc5a8eb..ef5c2d539f912 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1168,11 +1168,6 @@ def test_constructor_list_of_odicts(self): OrderedDict([["b", 3], ["c", 4], ["d", 6]]), ] - # single row dataframe - result = DataFrame([data[0]]) - expected = DataFrame.from_dict(dict(zip([0], [data[0]])), orient="index") - tm.assert_frame_equal(result, expected.reindex(result.index)) - result = DataFrame(data) expected = DataFrame.from_dict( dict(zip(range(len(data)), data)), orient="index" @@ -1183,6 +1178,13 @@ def test_constructor_list_of_odicts(self): expected = DataFrame(index=[0]) tm.assert_frame_equal(result, expected) + def test_constructor_single_row(self): + data = [OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]])] + + result = DataFrame(data) + expected = DataFrame.from_dict(dict(zip([0], data)), orient="index") + tm.assert_frame_equal(result, expected.reindex(result.index)) + def test_constructor_ordered_dict_preserve_order(self): # see gh-13304 expected = DataFrame([[2, 1]], columns=["b", "a"]) From 97fb2c119696e3c0425f47135e10bb57f845f48e Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sat, 11 Jul 2020 15:11:26 +0200 Subject: [PATCH 09/11] TST verify return none inplace for indexing/ --- .../indexing/multiindex/test_chaining_and_caching.py | 3 ++- pandas/tests/indexing/multiindex/test_indexing_slow.py | 9 ++++++--- pandas/tests/indexing/multiindex/test_ix.py | 3 ++- pandas/tests/indexing/multiindex/test_sorted.py | 6 ++++-- pandas/tests/indexing/multiindex/test_xs.py | 6 ++++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py index d3b13336e2a44..d6556a70b95a3 100644 --- a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py +++ b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py @@ -25,7 +25,8 @@ def test_detect_chained_assignment(): msg = "A value is trying to be set on a copy of a slice from a DataFrame" with pytest.raises(com.SettingWithCopyError, match=msg): - zed["eyes"]["right"].fillna(value=555, inplace=True) + return_value = zed["eyes"]["right"].fillna(value=555, inplace=True) + assert return_value is None def test_cache_updating(): diff --git a/pandas/tests/indexing/multiindex/test_indexing_slow.py b/pandas/tests/indexing/multiindex/test_indexing_slow.py index ea4453b8dd6eb..be193e0854d8d 100644 --- a/pandas/tests/indexing/multiindex/test_indexing_slow.py +++ b/pandas/tests/indexing/multiindex/test_indexing_slow.py @@ -34,12 +34,15 @@ def validate(mi, df, key): right = df[mask].copy() if i + 1 != len(key): # partial key - right.drop(cols[: i + 1], axis=1, inplace=True) - right.set_index(cols[i + 1 : -1], inplace=True) + return_value = right.drop(cols[: i + 1], axis=1, inplace=True) + assert return_value is None + return_value = right.set_index(cols[i + 1 : -1], inplace=True) + assert return_value is None tm.assert_frame_equal(mi.loc[key[: i + 1]], right) else: # full key - right.set_index(cols[:-1], inplace=True) + return_value = right.set_index(cols[:-1], inplace=True) + assert return_value is None if len(right) == 1: # single hit right = Series( right["jolia"].values, name=right.index[0], index=["jolia"] diff --git a/pandas/tests/indexing/multiindex/test_ix.py b/pandas/tests/indexing/multiindex/test_ix.py index 01b0b392d52a3..abf989324e4a5 100644 --- a/pandas/tests/indexing/multiindex/test_ix.py +++ b/pandas/tests/indexing/multiindex/test_ix.py @@ -35,7 +35,8 @@ def test_loc_general(self): tm.assert_frame_equal(df.loc[key], df.iloc[2:]) # this is ok - df.sort_index(inplace=True) + return_value = df.sort_index(inplace=True) + assert return_value is None res = df.loc[key] # col has float dtype, result should be Float64Index diff --git a/pandas/tests/indexing/multiindex/test_sorted.py b/pandas/tests/indexing/multiindex/test_sorted.py index fdeb3ce95b0bb..572cb9da405d1 100644 --- a/pandas/tests/indexing/multiindex/test_sorted.py +++ b/pandas/tests/indexing/multiindex/test_sorted.py @@ -43,8 +43,10 @@ def test_frame_getitem_not_sorted2(self, key): df2 = df.set_index(["col1", "col2"]) df2_original = df2.copy() - df2.index.set_levels(["b", "d", "a"], level="col1", inplace=True) - df2.index.set_codes([0, 1, 0, 2], level="col1", inplace=True) + return_value = df2.index.set_levels(["b", "d", "a"], level="col1", inplace=True) + assert return_value is None + return_value = df2.index.set_codes([0, 1, 0, 2], level="col1", inplace=True) + assert return_value is None assert not df2.index.is_lexsorted() assert not df2.index.is_monotonic diff --git a/pandas/tests/indexing/multiindex/test_xs.py b/pandas/tests/indexing/multiindex/test_xs.py index ff748d755c063..b807795b9c309 100644 --- a/pandas/tests/indexing/multiindex/test_xs.py +++ b/pandas/tests/indexing/multiindex/test_xs.py @@ -237,9 +237,11 @@ def test_series_getitem_multiindex_xs_by_label(): [("a", "one"), ("a", "two"), ("b", "one"), ("b", "two")] ) s = Series([1, 2, 3, 4], index=idx) - s.index.set_names(["L1", "L2"], inplace=True) + return_value = s.index.set_names(["L1", "L2"], inplace=True) + assert return_value is None expected = Series([1, 3], index=["a", "b"]) - expected.index.set_names(["L1"], inplace=True) + return_value = expected.index.set_names(["L1"], inplace=True) + assert return_value is None result = s.xs("one", level="L2") tm.assert_series_equal(result, expected) From 7446d9f09d7dbcde2b4b3a39a8446b49b6140c35 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sat, 11 Jul 2020 17:43:40 +0200 Subject: [PATCH 10/11] CLN fix --- pandas/tests/indexing/multiindex/test_chaining_and_caching.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py index d6556a70b95a3..d3b13336e2a44 100644 --- a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py +++ b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py @@ -25,8 +25,7 @@ def test_detect_chained_assignment(): msg = "A value is trying to be set on a copy of a slice from a DataFrame" with pytest.raises(com.SettingWithCopyError, match=msg): - return_value = zed["eyes"]["right"].fillna(value=555, inplace=True) - assert return_value is None + zed["eyes"]["right"].fillna(value=555, inplace=True) def test_cache_updating(): From 9f6b75ef07a02e9011408d73d098b55360068398 Mon Sep 17 00:00:00 2001 From: r-toroxel Date: Sun, 12 Jul 2020 10:07:43 +0200 Subject: [PATCH 11/11] CLN expected assignment --- pandas/tests/frame/test_constructors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index ef5c2d539f912..2a03828945a61 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1182,8 +1182,10 @@ def test_constructor_single_row(self): data = [OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]])] result = DataFrame(data) - expected = DataFrame.from_dict(dict(zip([0], data)), orient="index") - tm.assert_frame_equal(result, expected.reindex(result.index)) + expected = DataFrame.from_dict(dict(zip([0], data)), orient="index").reindex( + result.index + ) + tm.assert_frame_equal(result, expected) def test_constructor_ordered_dict_preserve_order(self): # see gh-13304