From 17aa2ba3f9b75bb4083f469b3649e18b80a5352c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 6 Feb 2024 02:29:12 +0000 Subject: [PATCH] CoW: Remove cow branches from more tests (#57270) --- .../multiindex/test_chaining_and_caching.py | 8 +- .../tests/indexing/multiindex/test_partial.py | 16 +--- .../tests/indexing/multiindex/test_setitem.py | 30 ++----- .../indexing/test_chaining_and_caching.py | 81 +++++-------------- pandas/tests/indexing/test_iat.py | 18 ----- pandas/tests/indexing/test_iloc.py | 32 +++----- pandas/tests/indexing/test_loc.py | 30 ++----- pandas/tests/series/indexing/test_indexing.py | 32 ++------ pandas/tests/series/indexing/test_setitem.py | 17 ++-- pandas/tests/series/methods/test_align.py | 22 ++--- pandas/tests/series/methods/test_copy.py | 53 +++++------- .../series/methods/test_get_numeric_data.py | 7 +- pandas/tests/series/methods/test_rename.py | 10 +-- .../tests/series/methods/test_sort_values.py | 2 +- pandas/tests/series/methods/test_update.py | 16 +--- pandas/tests/series/test_constructors.py | 14 +--- 16 files changed, 102 insertions(+), 286 deletions(-) diff --git a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py index c70c0ee10afd6..43aec12055cd2 100644 --- a/pandas/tests/indexing/multiindex/test_chaining_and_caching.py +++ b/pandas/tests/indexing/multiindex/test_chaining_and_caching.py @@ -31,7 +31,7 @@ def test_detect_chained_assignment(): zed["eyes"]["right"].fillna(value=555, inplace=True) -def test_cache_updating(using_copy_on_write): +def test_cache_updating(): # 5216 # make sure that we don't try to set a dead cache a = np.random.default_rng(2).random((10, 3)) @@ -47,11 +47,7 @@ def test_cache_updating(using_copy_on_write): with tm.raises_chained_assignment_error(): df.loc[0]["z"].iloc[0] = 1.0 - if using_copy_on_write: - assert df.loc[(0, 0), "z"] == df_original.loc[0, "z"] - else: - result = df.loc[(0, 0), "z"] - assert result == 1 + assert df.loc[(0, 0), "z"] == df_original.loc[0, "z"] # correct setting df.loc[(0, 0), "z"] = 2 diff --git a/pandas/tests/indexing/multiindex/test_partial.py b/pandas/tests/indexing/multiindex/test_partial.py index b68ab18fbc9b2..dbfabf7666d25 100644 --- a/pandas/tests/indexing/multiindex/test_partial.py +++ b/pandas/tests/indexing/multiindex/test_partial.py @@ -119,7 +119,6 @@ def test_getitem_partial_column_select(self): def test_partial_set( self, multiindex_year_month_day_dataframe_random_data, - using_copy_on_write, ): # GH #397 ymd = multiindex_year_month_day_dataframe_random_data @@ -129,13 +128,9 @@ def test_partial_set( exp.iloc[65:85] = 0 tm.assert_frame_equal(df, exp) - if using_copy_on_write: - with tm.raises_chained_assignment_error(): - df["A"].loc[2000, 4] = 1 - df.loc[(2000, 4), "A"] = 1 - else: - with tm.raises_chained_assignment_error(): - df["A"].loc[2000, 4] = 1 + with tm.raises_chained_assignment_error(): + df["A"].loc[2000, 4] = 1 + df.loc[(2000, 4), "A"] = 1 exp.iloc[65:85, 0] = 1 tm.assert_frame_equal(df, exp) @@ -146,10 +141,7 @@ def test_partial_set( # this works...for now with tm.raises_chained_assignment_error(): df["A"].iloc[14] = 5 - if using_copy_on_write: - assert df["A"].iloc[14] == exp["A"].iloc[14] - else: - assert df["A"].iloc[14] == 5 + assert df["A"].iloc[14] == exp["A"].iloc[14] @pytest.mark.parametrize("dtype", [int, float]) def test_getitem_intkey_leading_level( diff --git a/pandas/tests/indexing/multiindex/test_setitem.py b/pandas/tests/indexing/multiindex/test_setitem.py index d731f796637ea..1b6e1341a9c40 100644 --- a/pandas/tests/indexing/multiindex/test_setitem.py +++ b/pandas/tests/indexing/multiindex/test_setitem.py @@ -194,7 +194,7 @@ def test_multiindex_assignment(self): df.loc[4, "d"] = arr tm.assert_series_equal(df.loc[4, "d"], Series(arr, index=[8, 10], name="d")) - def test_multiindex_assignment_single_dtype(self, using_copy_on_write): + def test_multiindex_assignment_single_dtype(self): # GH3777 part 2b # single dtype arr = np.array([0.0, 1.0]) @@ -205,7 +205,6 @@ def test_multiindex_assignment_single_dtype(self, using_copy_on_write): index=[[4, 4, 8], [8, 10, 12]], dtype=np.int64, ) - view = df["c"].iloc[:2].values # arr can be losslessly cast to int, so this setitem is inplace # INFO(CoW-warn) this does not warn because we directly took .values @@ -215,10 +214,6 @@ def test_multiindex_assignment_single_dtype(self, using_copy_on_write): result = df.loc[4, "c"] tm.assert_series_equal(result, exp) - # extra check for inplace-ness - if not using_copy_on_write: - tm.assert_numpy_array_equal(view, exp.values) - # arr + 0.5 cannot be cast losslessly to int, so we upcast with tm.assert_produces_warning( FutureWarning, match="item of incompatible dtype" @@ -412,9 +407,7 @@ def test_setitem_change_dtype(self, multiindex_dataframe_random_data): reindexed = dft.reindex(columns=[("foo", "two")]) tm.assert_series_equal(reindexed["foo", "two"], s > s.median()) - def test_set_column_scalar_with_loc( - self, multiindex_dataframe_random_data, using_copy_on_write - ): + def test_set_column_scalar_with_loc(self, multiindex_dataframe_random_data): frame = multiindex_dataframe_random_data subset = frame.index[[1, 4, 5]] @@ -424,11 +417,8 @@ def test_set_column_scalar_with_loc( frame_original = frame.copy() col = frame["B"] col[subset] = 97 - if using_copy_on_write: - # chained setitem doesn't work with CoW - tm.assert_frame_equal(frame, frame_original) - else: - assert (frame.loc[subset, "B"] == 97).all() + # chained setitem doesn't work with CoW + tm.assert_frame_equal(frame, frame_original) def test_nonunique_assignment_1750(self): df = DataFrame( @@ -505,19 +495,13 @@ def test_setitem_enlargement_keep_index_names(self): tm.assert_frame_equal(df, expected) -def test_frame_setitem_view_direct( - multiindex_dataframe_random_data, using_copy_on_write -): +def test_frame_setitem_view_direct(multiindex_dataframe_random_data): # this works because we are modifying the underlying array # really a no-no df = multiindex_dataframe_random_data.T - if using_copy_on_write: - with pytest.raises(ValueError, match="read-only"): - df["foo"].values[:] = 0 - assert (df["foo"].values != 0).all() - else: + with pytest.raises(ValueError, match="read-only"): df["foo"].values[:] = 0 - assert (df["foo"].values == 0).all() + assert (df["foo"].values != 0).all() def test_frame_setitem_copy_raises(multiindex_dataframe_random_data): diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 7945d88c4a7dc..718ea69960775 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -27,7 +27,7 @@ def random_text(nobs=100): class TestCaching: - def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write): + def test_slice_consolidate_invalidate_item_cache(self): # this is chained assignment, but will 'work' with option_context("chained_assignment", None): # #3970 @@ -61,7 +61,7 @@ def test_setitem_cache_updating(self, do_ref): assert df.loc[0, "c"] == 0.0 assert df.loc[7, "c"] == 1.0 - def test_setitem_cache_updating_slices(self, using_copy_on_write): + def test_setitem_cache_updating_slices(self): # GH 7084 # not updating cache on series setting with slices expected = DataFrame( @@ -85,15 +85,11 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write): out_original = out.copy() for ix, row in df.iterrows(): v = out[row["C"]][six:eix] + row["D"] - with tm.raises_chained_assignment_error((ix == 0) or using_copy_on_write): + with tm.raises_chained_assignment_error(): out[row["C"]][six:eix] = v - if not using_copy_on_write: - tm.assert_frame_equal(out, expected) - tm.assert_series_equal(out["A"], expected["A"]) - else: - tm.assert_frame_equal(out, out_original) - tm.assert_series_equal(out["A"], out_original["A"]) + tm.assert_frame_equal(out, out_original) + tm.assert_series_equal(out["A"], out_original["A"]) out = DataFrame({"A": [0, 0, 0]}, index=date_range("5/7/2014", "5/9/2014")) for ix, row in df.iterrows(): @@ -102,7 +98,7 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write): tm.assert_frame_equal(out, expected) tm.assert_series_equal(out["A"], expected["A"]) - def test_altering_series_clears_parent_cache(self, using_copy_on_write): + def test_altering_series_clears_parent_cache(self): # GH #33675 df = DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["A", "B"]) ser = df["A"] @@ -116,49 +112,36 @@ def test_altering_series_clears_parent_cache(self, using_copy_on_write): class TestChaining: - def test_setitem_chained_setfault(self, using_copy_on_write): + def test_setitem_chained_setfault(self): # GH6026 data = ["right", "left", "left", "left", "right", "left", "timeout"] - mdata = ["right", "left", "left", "left", "right", "left", "none"] df = DataFrame({"response": np.array(data)}) mask = df.response == "timeout" with tm.raises_chained_assignment_error(): df.response[mask] = "none" - if using_copy_on_write: - tm.assert_frame_equal(df, DataFrame({"response": data})) - else: - tm.assert_frame_equal(df, DataFrame({"response": mdata})) + tm.assert_frame_equal(df, DataFrame({"response": data})) recarray = np.rec.fromarrays([data], names=["response"]) df = DataFrame(recarray) mask = df.response == "timeout" with tm.raises_chained_assignment_error(): df.response[mask] = "none" - if using_copy_on_write: - tm.assert_frame_equal(df, DataFrame({"response": data})) - else: - tm.assert_frame_equal(df, DataFrame({"response": mdata})) + tm.assert_frame_equal(df, DataFrame({"response": data})) df = DataFrame({"response": data, "response1": data}) df_original = df.copy() mask = df.response == "timeout" with tm.raises_chained_assignment_error(): df.response[mask] = "none" - if using_copy_on_write: - tm.assert_frame_equal(df, df_original) - else: - tm.assert_frame_equal(df, DataFrame({"response": mdata, "response1": data})) + tm.assert_frame_equal(df, df_original) # GH 6056 expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]}) df = DataFrame({"A": np.array(["foo", "bar", "bah", "foo", "bar"])}) with tm.raises_chained_assignment_error(): df["A"].iloc[0] = np.nan - if using_copy_on_write: - expected = DataFrame({"A": ["foo", "bar", "bah", "foo", "bar"]}) - else: - expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]}) + expected = DataFrame({"A": ["foo", "bar", "bah", "foo", "bar"]}) result = df.head() tm.assert_frame_equal(result, expected) @@ -169,10 +152,9 @@ def test_setitem_chained_setfault(self, using_copy_on_write): tm.assert_frame_equal(result, expected) @pytest.mark.arm_slow - def test_detect_chained_assignment(self, using_copy_on_write): + def test_detect_chained_assignment(self): with option_context("chained_assignment", "raise"): # work with the chain - expected = DataFrame([[-5, 1], [-6, 3]], columns=list("AB")) df = DataFrame( np.arange(4).reshape(2, 2), columns=list("AB"), dtype="int64" ) @@ -182,10 +164,7 @@ def test_detect_chained_assignment(self, using_copy_on_write): df["A"][0] = -5 with tm.raises_chained_assignment_error(): df["A"][1] = -6 - if using_copy_on_write: - tm.assert_frame_equal(df, df_original) - else: - tm.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, df_original) @pytest.mark.arm_slow def test_detect_chained_assignment_raises(self): @@ -340,9 +319,7 @@ def test_detect_chained_assignment_warnings_errors(self): df.loc[0]["A"] = 111 @pytest.mark.parametrize("rhs", [3, DataFrame({0: [1, 2, 3, 4]})]) - def test_detect_chained_assignment_warning_stacklevel( - self, rhs, using_copy_on_write - ): + def test_detect_chained_assignment_warning_stacklevel(self, rhs): # GH#42570 df = DataFrame(np.arange(25).reshape(5, 5)) df_original = df.copy() @@ -379,7 +356,7 @@ def test_cache_updating(self): assert "Hello Friend" in df["A"].index assert "Hello Friend" in df["B"].index - def test_cache_updating2(self, using_copy_on_write): + def test_cache_updating2(self): # 10264 df = DataFrame( np.zeros((5, 5), dtype="int64"), @@ -388,26 +365,11 @@ def test_cache_updating2(self, using_copy_on_write): ) df["f"] = 0 df_orig = df.copy() - if using_copy_on_write: - with pytest.raises(ValueError, match="read-only"): - df.f.values[3] = 1 - tm.assert_frame_equal(df, df_orig) - return - - df.f.values[3] = 1 - - df.f.values[3] = 2 - expected = DataFrame( - np.zeros((5, 6), dtype="int64"), - columns=["a", "b", "c", "d", "e", "f"], - index=range(5), - ) - expected.at[3, "f"] = 2 - tm.assert_frame_equal(df, expected) - expected = Series([0, 0, 0, 2, 0], name="f") - tm.assert_series_equal(df.f, expected) + with pytest.raises(ValueError, match="read-only"): + df.f.values[3] = 1 + tm.assert_frame_equal(df, df_orig) - def test_iloc_setitem_chained_assignment(self, using_copy_on_write): + def test_iloc_setitem_chained_assignment(self): # GH#3970 with option_context("chained_assignment", None): df = DataFrame({"aa": range(5), "bb": [2.2] * 5}) @@ -424,10 +386,7 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write): with tm.raises_chained_assignment_error(): df["bb"].iloc[0] = 0.15 - if not using_copy_on_write: - assert df["bb"].iloc[0] == 0.15 - else: - assert df["bb"].iloc[0] == 2.2 + assert df["bb"].iloc[0] == 2.2 def test_getitem_loc_assignment_slice_state(self): # GH 13569 diff --git a/pandas/tests/indexing/test_iat.py b/pandas/tests/indexing/test_iat.py index 4497c16efdfda..bb9252a50eb2a 100644 --- a/pandas/tests/indexing/test_iat.py +++ b/pandas/tests/indexing/test_iat.py @@ -28,21 +28,3 @@ def test_iat_getitem_series_with_period_index(): expected = ser[index[0]] result = ser.iat[0] assert expected == result - - -def test_iat_setitem_item_cache_cleared(indexer_ial, using_copy_on_write): - # GH#45684 - data = {"x": np.arange(8, dtype=np.int64), "y": np.int64(0)} - df = DataFrame(data).copy() - ser = df["y"] - - # previously this iat setting would split the block and fail to clear - # the item_cache. - indexer_ial(df)[7, 0] = 9999 - - indexer_ial(df)[7, 1] = 1234 - - assert df.iat[7, 1] == 1234 - if not using_copy_on_write: - assert ser.iloc[-1] == 1234 - assert df.iloc[-1, -1] == 1234 diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 5453c8be0e832..8650a1afb383d 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -105,9 +105,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer_li, key): expected = DataFrame({0: Series(cat.astype(object), dtype=object), 1: range(3)}) tm.assert_frame_equal(df, expected) - def test_iloc_setitem_ea_inplace( - self, frame_or_series, index_or_series_or_array, using_copy_on_write - ): + def test_iloc_setitem_ea_inplace(self, frame_or_series, index_or_series_or_array): # GH#38952 Case with not setting a full column # IntegerArray without NAs arr = array([1, 2, 3, 4]) @@ -128,11 +126,8 @@ def test_iloc_setitem_ea_inplace( # Check that we are actually in-place if frame_or_series is Series: - if using_copy_on_write: - assert obj.values is not values - assert np.shares_memory(obj.values, values) - else: - assert obj.values is values + assert obj.values is not values + assert np.shares_memory(obj.values, values) else: assert np.shares_memory(obj[0].values, values) @@ -843,7 +838,7 @@ def test_iloc_empty_list_indexer_is_ok(self): df.iloc[[]], df.iloc[:0, :], check_index_type=True, check_column_type=True ) - def test_identity_slice_returns_new_object(self, using_copy_on_write): + def test_identity_slice_returns_new_object(self): # GH13873 original_df = DataFrame({"a": [1, 2, 3]}) sliced_df = original_df.iloc[:] @@ -855,10 +850,7 @@ def test_identity_slice_returns_new_object(self, using_copy_on_write): # Setting using .loc[:, "a"] sets inplace so alters both sliced and orig # depending on CoW original_df.loc[:, "a"] = [4, 4, 4] - if using_copy_on_write: - assert (sliced_df["a"] == [1, 2, 3]).all() - else: - assert (sliced_df["a"] == 4).all() + assert (sliced_df["a"] == [1, 2, 3]).all() original_series = Series([1, 2, 3, 4, 5, 6]) sliced_series = original_series.iloc[:] @@ -866,11 +858,8 @@ def test_identity_slice_returns_new_object(self, using_copy_on_write): # should also be a shallow copy original_series[:3] = [7, 8, 9] - if using_copy_on_write: - # shallow copy not updated (CoW) - assert all(sliced_series[:3] == [1, 2, 3]) - else: - assert all(sliced_series[:3] == [7, 8, 9]) + # shallow copy not updated (CoW) + assert all(sliced_series[:3] == [1, 2, 3]) def test_indexing_zerodim_np_array(self): # GH24919 @@ -1414,7 +1403,7 @@ def test_frame_iloc_setitem_callable(self): class TestILocSeries: - def test_iloc(self, using_copy_on_write): + def test_iloc(self): ser = Series( np.random.default_rng(2).standard_normal(10), index=list(range(0, 20, 2)) ) @@ -1434,10 +1423,7 @@ def test_iloc(self, using_copy_on_write): with tm.assert_produces_warning(None): # GH#45324 make sure we aren't giving a spurious FutureWarning result[:] = 0 - if using_copy_on_write: - tm.assert_series_equal(ser, ser_original) - else: - assert (ser.iloc[1:3] == 0).all() + tm.assert_series_equal(ser, ser_original) # list of integers result = ser.iloc[[0, 2, 3, 4, 5]] diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 4f70c63aeb353..f263f92b4f0eb 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1089,7 +1089,7 @@ def test_loc_empty_list_indexer_is_ok(self): df.loc[[]], df.iloc[:0, :], check_index_type=True, check_column_type=True ) - def test_identity_slice_returns_new_object(self, using_copy_on_write): + def test_identity_slice_returns_new_object(self): # GH13873 original_df = DataFrame({"a": [1, 2, 3]}) @@ -1104,17 +1104,11 @@ def test_identity_slice_returns_new_object(self, using_copy_on_write): # Setting using .loc[:, "a"] sets inplace so alters both sliced and orig # depending on CoW original_df.loc[:, "a"] = [4, 4, 4] - if using_copy_on_write: - assert (sliced_df["a"] == [1, 2, 3]).all() - else: - assert (sliced_df["a"] == 4).all() + assert (sliced_df["a"] == [1, 2, 3]).all() # These should not return copies df = DataFrame(np.random.default_rng(2).standard_normal((10, 4))) - if using_copy_on_write: - assert df[0] is not df.loc[:, 0] - else: - assert df[0] is df.loc[:, 0] + assert df[0] is not df.loc[:, 0] # Same tests for Series original_series = Series([1, 2, 3, 4, 5, 6]) @@ -1123,17 +1117,10 @@ def test_identity_slice_returns_new_object(self, using_copy_on_write): assert original_series[:] is not original_series original_series[:3] = [7, 8, 9] - if using_copy_on_write: - assert all(sliced_series[:3] == [1, 2, 3]) - else: - assert all(sliced_series[:3] == [7, 8, 9]) + assert all(sliced_series[:3] == [1, 2, 3]) - def test_loc_copy_vs_view(self, request, using_copy_on_write): + def test_loc_copy_vs_view(self, request): # GH 15631 - - if not using_copy_on_write: - mark = pytest.mark.xfail(reason="accidental fix reverted - GH37497") - request.applymarker(mark) x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) y = x.copy() @@ -2634,7 +2621,7 @@ def test_loc_setitem_boolean_and_column(self, float_frame): expected = DataFrame(values, index=expected.index, columns=expected.columns) tm.assert_frame_equal(float_frame, expected) - def test_loc_setitem_ndframe_values_alignment(self, using_copy_on_write): + def test_loc_setitem_ndframe_values_alignment(self): # GH#45501 df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) df.loc[[False, False, True], ["a"]] = DataFrame( @@ -2658,10 +2645,7 @@ def test_loc_setitem_ndframe_values_alignment(self, using_copy_on_write): df_orig = df.copy() ser = df["a"] ser.loc[[False, False, True]] = Series([10, 11, 12], index=[2, 1, 0]) - if using_copy_on_write: - tm.assert_frame_equal(df, df_orig) - else: - tm.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, df_orig) def test_loc_indexer_empty_broadcast(self): # GH#51450 diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 50c167f3f3a28..5c36877e5ac86 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -101,16 +101,12 @@ def test_basic_getitem_dt64tz_values(): assert result == expected -def test_getitem_setitem_ellipsis(using_copy_on_write): +def test_getitem_setitem_ellipsis(): s = Series(np.random.default_rng(2).standard_normal(10)) result = s[...] tm.assert_series_equal(result, s) - s[...] = 5 - if not using_copy_on_write: - assert (result == 5).all() - @pytest.mark.parametrize( "result_1, duplicate_item, expected_1", @@ -242,7 +238,7 @@ def test_basic_getitem_setitem_corner(datetime_series): datetime_series[[5, [None, None]]] = 2 -def test_slice(string_series, object_series, using_copy_on_write): +def test_slice(string_series, object_series): original = string_series.copy() numSlice = string_series[10:20] numSliceEnd = string_series[-10:] @@ -261,11 +257,8 @@ def test_slice(string_series, object_series, using_copy_on_write): sl = string_series[10:20] sl[:] = 0 - if using_copy_on_write: - # Doesn't modify parent (CoW) - tm.assert_series_equal(string_series, original) - else: - assert (string_series[10:20] == 0).all() + # Doesn't modify parent (CoW) + tm.assert_series_equal(string_series, original) def test_timedelta_assignment(): @@ -282,7 +275,7 @@ def test_timedelta_assignment(): tm.assert_series_equal(s, expected) -def test_underlying_data_conversion(using_copy_on_write): +def test_underlying_data_conversion(): # GH 4080 df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]}) return_value = df.set_index(["a", "b", "c"], inplace=True) @@ -292,18 +285,9 @@ def test_underlying_data_conversion(using_copy_on_write): df_original = df.copy() df - if using_copy_on_write: - with tm.raises_chained_assignment_error(): - df["val"].update(s) - expected = df_original - else: - with tm.assert_produces_warning(FutureWarning, match="inplace method"): - df["val"].update(s) - expected = DataFrame( - {"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3], "val": [0, 1, 0]} - ) - return_value = expected.set_index(["a", "b", "c"], inplace=True) - assert return_value is None + with tm.raises_chained_assignment_error(): + df["val"].update(s) + expected = df_original tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 85ffb0f8fe647..6be325073bb67 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -437,7 +437,7 @@ def test_setitem_mask_smallint_no_upcast(self): class TestSetitemViewCopySemantics: - def test_setitem_invalidates_datetime_index_freq(self, using_copy_on_write): + def test_setitem_invalidates_datetime_index_freq(self): # GH#24096 altering a datetime64tz Series inplace invalidates the # `freq` attribute on the underlying DatetimeIndex @@ -445,10 +445,7 @@ def test_setitem_invalidates_datetime_index_freq(self, using_copy_on_write): ts = dti[1] ser = Series(dti) assert ser._values is not dti - if using_copy_on_write: - assert ser._values._ndarray.base is dti._data._ndarray.base - else: - assert ser._values._ndarray.base is not dti._data._ndarray.base + assert ser._values._ndarray.base is dti._data._ndarray.base assert dti.freq == "D" ser.iloc[1] = NaT assert ser._values.freq is None @@ -459,18 +456,14 @@ def test_setitem_invalidates_datetime_index_freq(self, using_copy_on_write): assert dti[1] == ts assert dti.freq == "D" - def test_dt64tz_setitem_does_not_mutate_dti(self, using_copy_on_write): + def test_dt64tz_setitem_does_not_mutate_dti(self): # GH#21907, GH#24096 dti = date_range("2016-01-01", periods=10, tz="US/Pacific") ts = dti[0] ser = Series(dti) assert ser._values is not dti - if using_copy_on_write: - assert ser._values._ndarray.base is dti._data._ndarray.base - assert ser._mgr.arrays[0]._ndarray.base is dti._data._ndarray.base - else: - assert ser._values._ndarray.base is not dti._data._ndarray.base - assert ser._mgr.arrays[0]._ndarray.base is not dti._data._ndarray.base + assert ser._values._ndarray.base is dti._data._ndarray.base + assert ser._mgr.arrays[0]._ndarray.base is dti._data._ndarray.base assert ser._mgr.arrays[0] is not dti diff --git a/pandas/tests/series/methods/test_align.py b/pandas/tests/series/methods/test_align.py index cb60cd2e5bcf3..df3dd6f4d8ab0 100644 --- a/pandas/tests/series/methods/test_align.py +++ b/pandas/tests/series/methods/test_align.py @@ -89,7 +89,7 @@ def test_align_fill_method( tm.assert_series_equal(ab, eb) -def test_align_nocopy(datetime_series, using_copy_on_write): +def test_align_nocopy(datetime_series): b = datetime_series[:5].copy() # do copy @@ -102,10 +102,7 @@ def test_align_nocopy(datetime_series, using_copy_on_write): a = datetime_series.copy() ra, _ = a.align(b, join="left", copy=False) ra[:5] = 5 - if using_copy_on_write: - assert not (a[:5] == 5).any() - else: - assert (a[:5] == 5).all() + assert not (a[:5] == 5).any() # do copy a = datetime_series.copy() @@ -119,20 +116,13 @@ def test_align_nocopy(datetime_series, using_copy_on_write): b = datetime_series[:5].copy() _, rb = a.align(b, join="right", copy=False) rb[:2] = 5 - if using_copy_on_write: - assert not (b[:2] == 5).any() - else: - assert (b[:2] == 5).all() + assert not (b[:2] == 5).any() -def test_align_same_index(datetime_series, using_copy_on_write): +def test_align_same_index(datetime_series): a, b = datetime_series.align(datetime_series, copy=False) - if not using_copy_on_write: - assert a.index is datetime_series.index - assert b.index is datetime_series.index - else: - assert a.index.is_(datetime_series.index) - assert b.index.is_(datetime_series.index) + assert a.index.is_(datetime_series.index) + assert b.index.is_(datetime_series.index) a, b = datetime_series.align(datetime_series, copy=True) assert a.index is not datetime_series.index diff --git a/pandas/tests/series/methods/test_copy.py b/pandas/tests/series/methods/test_copy.py index ea439fb5a3263..ad5417d330d51 100644 --- a/pandas/tests/series/methods/test_copy.py +++ b/pandas/tests/series/methods/test_copy.py @@ -10,7 +10,7 @@ class TestCopy: @pytest.mark.parametrize("deep", ["default", None, False, True]) - def test_copy(self, deep, using_copy_on_write): + def test_copy(self, deep): ser = Series(np.arange(10), dtype="float64") # default deep is True @@ -19,28 +19,22 @@ def test_copy(self, deep, using_copy_on_write): else: ser2 = ser.copy(deep=deep) - if using_copy_on_write: - # INFO(CoW) a shallow copy doesn't yet copy the data - # but parent will not be modified (CoW) - if deep is None or deep is False: - assert np.may_share_memory(ser.values, ser2.values) - else: - assert not np.may_share_memory(ser.values, ser2.values) + # INFO(CoW) a shallow copy doesn't yet copy the data + # but parent will not be modified (CoW) + if deep is None or deep is False: + assert np.may_share_memory(ser.values, ser2.values) + else: + assert not np.may_share_memory(ser.values, ser2.values) ser2[::2] = np.nan - if deep is not False or using_copy_on_write: - # Did not modify original Series - assert np.isnan(ser2[0]) - assert not np.isnan(ser[0]) - else: - # we DID modify the original Series - assert np.isnan(ser2[0]) - assert np.isnan(ser[0]) + # Did not modify original Series + assert np.isnan(ser2[0]) + assert not np.isnan(ser[0]) @pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning") @pytest.mark.parametrize("deep", ["default", None, False, True]) - def test_copy_tzaware(self, deep, using_copy_on_write): + def test_copy_tzaware(self, deep): # GH#11794 # copy of tz-aware expected = Series([Timestamp("2012/01/01", tz="UTC")]) @@ -53,25 +47,18 @@ def test_copy_tzaware(self, deep, using_copy_on_write): else: ser2 = ser.copy(deep=deep) - if using_copy_on_write: - # INFO(CoW) a shallow copy doesn't yet copy the data - # but parent will not be modified (CoW) - if deep is None or deep is False: - assert np.may_share_memory(ser.values, ser2.values) - else: - assert not np.may_share_memory(ser.values, ser2.values) + # INFO(CoW) a shallow copy doesn't yet copy the data + # but parent will not be modified (CoW) + if deep is None or deep is False: + assert np.may_share_memory(ser.values, ser2.values) + else: + assert not np.may_share_memory(ser.values, ser2.values) ser2[0] = Timestamp("1999/01/01", tz="UTC") - # default deep is True - if deep is not False or using_copy_on_write: - # Did not modify original Series - tm.assert_series_equal(ser2, expected2) - tm.assert_series_equal(ser, expected) - else: - # we DID modify the original Series - tm.assert_series_equal(ser2, expected2) - tm.assert_series_equal(ser, expected2) + # Did not modify original Series + tm.assert_series_equal(ser2, expected2) + tm.assert_series_equal(ser, expected) def test_copy_name(self, datetime_series): result = datetime_series.copy() diff --git a/pandas/tests/series/methods/test_get_numeric_data.py b/pandas/tests/series/methods/test_get_numeric_data.py index 11dc6d5c57162..f25583904377a 100644 --- a/pandas/tests/series/methods/test_get_numeric_data.py +++ b/pandas/tests/series/methods/test_get_numeric_data.py @@ -7,7 +7,7 @@ class TestGetNumericData: - def test_get_numeric_data_preserve_dtype(self, using_copy_on_write): + def test_get_numeric_data_preserve_dtype(self): # get the numeric data obj = Series([1, 2, 3]) result = obj._get_numeric_data() @@ -15,10 +15,7 @@ def test_get_numeric_data_preserve_dtype(self, using_copy_on_write): # returned object is a shallow copy result.iloc[0] = 0 - if using_copy_on_write: - assert obj.iloc[0] == 1 - else: - assert obj.iloc[0] == 0 + assert obj.iloc[0] == 1 obj = Series([1, "2", 3.0]) result = obj._get_numeric_data() diff --git a/pandas/tests/series/methods/test_rename.py b/pandas/tests/series/methods/test_rename.py index e59389ab069d3..c67298b777f6d 100644 --- a/pandas/tests/series/methods/test_rename.py +++ b/pandas/tests/series/methods/test_rename.py @@ -169,15 +169,11 @@ def test_rename_error_arg(self): with pytest.raises(KeyError, match=match): ser.rename({2: 9}, errors="raise") - def test_rename_copy_false(self, using_copy_on_write): + def test_rename_copy_false(self): # GH 46889 ser = Series(["foo", "bar"]) ser_orig = ser.copy() shallow_copy = ser.rename({1: 9}, copy=False) ser[0] = "foobar" - if using_copy_on_write: - assert ser_orig[0] == shallow_copy[0] - assert ser_orig[1] == shallow_copy[9] - else: - assert ser[0] == shallow_copy[0] - assert ser[1] == shallow_copy[9] + assert ser_orig[0] == shallow_copy[0] + assert ser_orig[1] == shallow_copy[9] diff --git a/pandas/tests/series/methods/test_sort_values.py b/pandas/tests/series/methods/test_sort_values.py index bd548eb80e182..995d9d176fc2b 100644 --- a/pandas/tests/series/methods/test_sort_values.py +++ b/pandas/tests/series/methods/test_sort_values.py @@ -10,7 +10,7 @@ class TestSeriesSortValues: - def test_sort_values(self, datetime_series, using_copy_on_write): + def test_sort_values(self, datetime_series): # check indexes are reordered corresponding with the values ser = Series([3, 2, 4, 1], ["A", "B", "C", "D"]) expected = Series([1, 2, 3, 4], ["D", "B", "A", "C"]) diff --git a/pandas/tests/series/methods/test_update.py b/pandas/tests/series/methods/test_update.py index 191aa36ad5d41..1d29e116be5c2 100644 --- a/pandas/tests/series/methods/test_update.py +++ b/pandas/tests/series/methods/test_update.py @@ -14,7 +14,7 @@ class TestUpdate: - def test_update(self, using_copy_on_write): + def test_update(self): s = Series([1.5, np.nan, 3.0, 4.0, np.nan]) s2 = Series([np.nan, 3.5, np.nan, 5.0]) s.update(s2) @@ -29,17 +29,9 @@ def test_update(self, using_copy_on_write): df["c"] = df["c"].astype(object) df_orig = df.copy() - if using_copy_on_write: - with tm.raises_chained_assignment_error(): - df["c"].update(Series(["foo"], index=[0])) - expected = df_orig - else: - with tm.assert_produces_warning(FutureWarning, match="inplace method"): - df["c"].update(Series(["foo"], index=[0])) - expected = DataFrame( - [[1, np.nan, "foo"], [3, 2.0, np.nan]], columns=["a", "b", "c"] - ) - expected["c"] = expected["c"].astype(object) + with tm.raises_chained_assignment_error(): + df["c"].update(Series(["foo"], index=[0])) + expected = df_orig tm.assert_frame_equal(df, expected) @pytest.mark.parametrize( diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index e17cf7491f58b..b00074c04257e 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -622,15 +622,12 @@ def test_constructor_maskedarray_hardened(self): expected = Series([np.nan, np.nan, np.nan]) tm.assert_series_equal(result, expected) - def test_series_ctor_plus_datetimeindex(self, using_copy_on_write): + def test_series_ctor_plus_datetimeindex(self): rng = date_range("20090415", "20090519", freq="B") data = {k: 1 for k in rng} result = Series(data, index=rng) - if using_copy_on_write: - assert result.index.is_(rng) - else: - assert result.index is rng + assert result.index.is_(rng) def test_constructor_default_index(self): s = Series([0, 1, 2]) @@ -891,16 +888,13 @@ def test_constructor_invalid_coerce_ints_with_float_nan(self, any_int_numpy_dtyp with pytest.raises(IntCastingNaNError, match=msg): Series(np.array(vals), dtype=any_int_numpy_dtype) - def test_constructor_dtype_no_cast(self, using_copy_on_write): + def test_constructor_dtype_no_cast(self): # see gh-1572 s = Series([1, 2, 3]) s2 = Series(s, dtype=np.int64) s2[1] = 5 - if using_copy_on_write: - assert s[1] == 2 - else: - assert s[1] == 5 + assert s[1] == 2 def test_constructor_datelike_coercion(self): # GH 9477