From 1307608a8c1cbe88a9002a5fd7ee050593d9a2ad Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 7 Nov 2023 16:43:42 +0100 Subject: [PATCH] TST: clean-up various tests avoiding CoW issues (#55861) --- pandas/tests/frame/indexing/test_indexing.py | 4 ++-- pandas/tests/frame/test_api.py | 6 ++---- pandas/tests/frame/test_nonunique_indexes.py | 4 ++-- pandas/tests/indexing/test_scalar.py | 2 +- pandas/tests/series/indexing/test_setitem.py | 2 +- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 58581941509e8..4bad0838f7af8 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -747,11 +747,11 @@ def test_getitem_setitem_float_labels(self, using_array_manager): expected = df.iloc[0:2] tm.assert_frame_equal(result, expected) - df.loc[1:2] = 0 + expected = df.iloc[0:2] msg = r"The behavior of obj\[i:j\] with a float-dtype index" with tm.assert_produces_warning(FutureWarning, match=msg): result = df[1:2] - assert (result == 0).all().all() + tm.assert_frame_equal(result, expected) # #2727 index = Index([1.0, 2.5, 3.5, 4.5, 5.0]) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 5648cb15ef2b4..04821d0865887 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -217,10 +217,8 @@ def test_with_datetimelikes(self): def test_deepcopy(self, float_frame): cp = deepcopy(float_frame) - series = cp["A"] - series[:] = 10 - for idx, value in series.items(): - assert float_frame["A"][idx] != value + cp.loc[0, "A"] = 10 + assert not float_frame.equals(cp) def test_inplace_return_self(self): # GH 1893 diff --git a/pandas/tests/frame/test_nonunique_indexes.py b/pandas/tests/frame/test_nonunique_indexes.py index 12aeede2560b8..34f172e900ab7 100644 --- a/pandas/tests/frame/test_nonunique_indexes.py +++ b/pandas/tests/frame/test_nonunique_indexes.py @@ -322,7 +322,7 @@ def test_set_value_by_index(self): df = DataFrame(np.arange(9).reshape(3, 3).T) df.columns = list("AAA") - expected = df.iloc[:, 2] + expected = df.iloc[:, 2].copy() with tm.assert_produces_warning(warn, match=msg): df.iloc[:, 0] = 3 @@ -330,7 +330,7 @@ def test_set_value_by_index(self): df = DataFrame(np.arange(9).reshape(3, 3).T) df.columns = [2, float(2), str(2)] - expected = df.iloc[:, 1] + expected = df.iloc[:, 1].copy() with tm.assert_produces_warning(warn, match=msg): df.iloc[:, 0] = 3 diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 2753b3574e583..5b95eb65ff00a 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -140,7 +140,7 @@ def test_frame_at_with_duplicate_axes(self): df = DataFrame(arr, columns=["A", "A"]) result = df.at[0, "A"] - expected = df.iloc[0] + expected = df.iloc[0].copy() tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 5fcd3a19dcaa4..16c127e6ece7b 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -1746,7 +1746,7 @@ def test_setitem_with_bool_indexer(): # GH#42530 df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) - result = df.pop("b") + result = df.pop("b").copy() result[[True, False, False]] = 9 expected = Series(data=[9, 5, 6], name="b") tm.assert_series_equal(result, expected)