Skip to content

Commit 46fd711

Browse files
authored
CoW: Remove copy-on-write conditions from frame tests (#57255)
1 parent 8848692 commit 46fd711

23 files changed

+129
-398
lines changed

pandas/tests/frame/indexing/test_getitem.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -391,18 +391,13 @@ def test_getitem_empty_frame_with_boolean(self):
391391
df2 = df[df > 0]
392392
tm.assert_frame_equal(df, df2)
393393

394-
def test_getitem_returns_view_when_column_is_unique_in_df(
395-
self, using_copy_on_write
396-
):
394+
def test_getitem_returns_view_when_column_is_unique_in_df(self):
397395
# GH#45316
398396
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
399397
df_orig = df.copy()
400398
view = df["b"]
401399
view.loc[:] = 100
402-
if using_copy_on_write:
403-
expected = df_orig
404-
else:
405-
expected = DataFrame([[1, 2, 100], [4, 5, 100]], columns=["a", "a", "b"])
400+
expected = df_orig
406401
tm.assert_frame_equal(df, expected)
407402

408403
def test_getitem_frozenset_unique_in_column(self):

pandas/tests/frame/indexing/test_indexing.py

+11-36
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,7 @@ def test_getitem_setitem_integer_slice_keyerrors(self):
565565
with pytest.raises(KeyError, match=r"^3$"):
566566
df2.loc[3:11] = 0
567567

568-
def test_fancy_getitem_slice_mixed(
569-
self, float_frame, float_string_frame, using_copy_on_write
570-
):
568+
def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):
571569
sliced = float_string_frame.iloc[:, -3:]
572570
assert sliced["D"].dtype == np.float64
573571

@@ -579,13 +577,7 @@ def test_fancy_getitem_slice_mixed(
579577
assert np.shares_memory(sliced["C"]._values, float_frame["C"]._values)
580578

581579
sliced.loc[:, "C"] = 4.0
582-
if not using_copy_on_write:
583-
assert (float_frame["C"] == 4).all()
584-
585-
# with the enforcement of GH#45333 in 2.0, this remains a view
586-
np.shares_memory(sliced["C"]._values, float_frame["C"]._values)
587-
else:
588-
tm.assert_frame_equal(float_frame, original)
580+
tm.assert_frame_equal(float_frame, original)
589581

590582
def test_getitem_setitem_non_ix_labels(self):
591583
df = DataFrame(range(20), index=date_range("2020-01-01", periods=20))
@@ -1053,7 +1045,7 @@ def test_iloc_row(self):
10531045
expected = df.reindex(df.index[[1, 2, 4, 6]])
10541046
tm.assert_frame_equal(result, expected)
10551047

1056-
def test_iloc_row_slice_view(self, using_copy_on_write):
1048+
def test_iloc_row_slice_view(self):
10571049
df = DataFrame(
10581050
np.random.default_rng(2).standard_normal((10, 4)), index=range(0, 20, 2)
10591051
)
@@ -1067,11 +1059,6 @@ def test_iloc_row_slice_view(self, using_copy_on_write):
10671059

10681060
exp_col = original[2].copy()
10691061
subset.loc[:, 2] = 0.0
1070-
if not using_copy_on_write:
1071-
exp_col._values[4:8] = 0.0
1072-
1073-
# With the enforcement of GH#45333 in 2.0, this remains a view
1074-
assert np.shares_memory(df[2], subset[2])
10751062
tm.assert_series_equal(df[2], exp_col)
10761063

10771064
def test_iloc_col(self):
@@ -1097,32 +1084,20 @@ def test_iloc_col(self):
10971084
expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
10981085
tm.assert_frame_equal(result, expected)
10991086

1100-
def test_iloc_col_slice_view(self, using_copy_on_write):
1087+
def test_iloc_col_slice_view(self):
11011088
df = DataFrame(
11021089
np.random.default_rng(2).standard_normal((4, 10)), columns=range(0, 20, 2)
11031090
)
11041091
original = df.copy()
11051092
subset = df.iloc[:, slice(4, 8)]
11061093

1107-
if not using_copy_on_write:
1108-
# verify slice is view
1109-
assert np.shares_memory(df[8]._values, subset[8]._values)
1110-
1111-
subset.loc[:, 8] = 0.0
1112-
1113-
assert (df[8] == 0).all()
1114-
1115-
# with the enforcement of GH#45333 in 2.0, this remains a view
1116-
assert np.shares_memory(df[8]._values, subset[8]._values)
1117-
else:
1118-
if using_copy_on_write:
1119-
# verify slice is view
1120-
assert np.shares_memory(df[8]._values, subset[8]._values)
1121-
subset[8] = 0.0
1122-
# subset changed
1123-
assert (subset[8] == 0).all()
1124-
# but df itself did not change (setitem replaces full column)
1125-
tm.assert_frame_equal(df, original)
1094+
# verify slice is view
1095+
assert np.shares_memory(df[8]._values, subset[8]._values)
1096+
subset[8] = 0.0
1097+
# subset changed
1098+
assert (subset[8] == 0).all()
1099+
# but df itself did not change (setitem replaces full column)
1100+
tm.assert_frame_equal(df, original)
11261101

11271102
def test_loc_duplicates(self):
11281103
# gh-17105

pandas/tests/frame/indexing/test_insert.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_insert_with_columns_dups(self):
7171
)
7272
tm.assert_frame_equal(df, exp)
7373

74-
def test_insert_item_cache(self, using_copy_on_write):
74+
def test_insert_item_cache(self):
7575
df = DataFrame(np.random.default_rng(2).standard_normal((4, 3)))
7676
ser = df[0]
7777
expected_warning = PerformanceWarning
@@ -80,14 +80,9 @@ def test_insert_item_cache(self, using_copy_on_write):
8080
for n in range(100):
8181
df[n + 3] = df[1] * n
8282

83-
if using_copy_on_write:
84-
ser.iloc[0] = 99
85-
assert df.iloc[0, 0] == df[0][0]
86-
assert df.iloc[0, 0] != 99
87-
else:
88-
ser.values[0] = 99
89-
assert df.iloc[0, 0] == df[0][0]
90-
assert df.iloc[0, 0] == 99
83+
ser.iloc[0] = 99
84+
assert df.iloc[0, 0] == df[0][0]
85+
assert df.iloc[0, 0] != 99
9186

9287
def test_insert_EA_no_warning(self):
9388
# PerformanceWarning about fragmented frame should not be raised when

pandas/tests/frame/indexing/test_setitem.py

+9-26
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def test_frame_setitem_existing_datetime64_col_other_units(self, unit):
324324
df["dates"] = vals
325325
assert (df["dates"].values == ex_vals).all()
326326

327-
def test_setitem_dt64tz(self, timezone_frame, using_copy_on_write):
327+
def test_setitem_dt64tz(self, timezone_frame):
328328
df = timezone_frame
329329
idx = df["B"].rename("foo")
330330

@@ -345,10 +345,7 @@ def test_setitem_dt64tz(self, timezone_frame, using_copy_on_write):
345345
tm.assert_extension_array_equal(v1, v2)
346346
v1base = v1._ndarray.base
347347
v2base = v2._ndarray.base
348-
if not using_copy_on_write:
349-
assert v1base is None or (id(v1base) != id(v2base))
350-
else:
351-
assert id(v1base) == id(v2base)
348+
assert id(v1base) == id(v2base)
352349

353350
# with nan
354351
df2 = df.copy()
@@ -844,7 +841,7 @@ def test_setitem_object_array_of_tzaware_datetimes(self, idx, expected):
844841

845842

846843
class TestDataFrameSetItemWithExpansion:
847-
def test_setitem_listlike_views(self, using_copy_on_write):
844+
def test_setitem_listlike_views(self):
848845
# GH#38148
849846
df = DataFrame({"a": [1, 2, 3], "b": [4, 4, 6]})
850847

@@ -857,10 +854,7 @@ def test_setitem_listlike_views(self, using_copy_on_write):
857854
# edit in place the first column to check view semantics
858855
df.iloc[0, 0] = 100
859856

860-
if using_copy_on_write:
861-
expected = Series([1, 2, 3], name="a")
862-
else:
863-
expected = Series([100, 2, 3], name="a")
857+
expected = Series([1, 2, 3], name="a")
864858
tm.assert_series_equal(ser, expected)
865859

866860
def test_setitem_string_column_numpy_dtype_raising(self):
@@ -870,7 +864,7 @@ def test_setitem_string_column_numpy_dtype_raising(self):
870864
expected = DataFrame([[1, 2, 5], [3, 4, 6]], columns=[0, 1, "0 - Name"])
871865
tm.assert_frame_equal(df, expected)
872866

873-
def test_setitem_empty_df_duplicate_columns(self, using_copy_on_write):
867+
def test_setitem_empty_df_duplicate_columns(self):
874868
# GH#38521
875869
df = DataFrame(columns=["a", "b", "b"], dtype="float64")
876870
df.loc[:, "a"] = list(range(2))
@@ -1199,7 +1193,7 @@ def test_setitem_always_copy(self, float_frame):
11991193
assert notna(s[5:10]).all()
12001194

12011195
@pytest.mark.parametrize("consolidate", [True, False])
1202-
def test_setitem_partial_column_inplace(self, consolidate, using_copy_on_write):
1196+
def test_setitem_partial_column_inplace(self, consolidate):
12031197
# This setting should be in-place, regardless of whether frame is
12041198
# single-block or multi-block
12051199
# GH#304 this used to be incorrectly not-inplace, in which case
@@ -1215,18 +1209,11 @@ def test_setitem_partial_column_inplace(self, consolidate, using_copy_on_write):
12151209
else:
12161210
assert len(df._mgr.blocks) == 2
12171211

1218-
zvals = df["z"]._values
1219-
12201212
df.loc[2:, "z"] = 42
12211213

12221214
expected = Series([np.nan, np.nan, 42, 42], index=df.index, name="z")
12231215
tm.assert_series_equal(df["z"], expected)
12241216

1225-
# check setting occurred in-place
1226-
if not using_copy_on_write:
1227-
tm.assert_numpy_array_equal(zvals, expected.values)
1228-
assert np.shares_memory(zvals, df["z"]._values)
1229-
12301217
def test_setitem_duplicate_columns_not_inplace(self):
12311218
# GH#39510
12321219
cols = ["A", "B"] * 2
@@ -1298,7 +1285,7 @@ def test_setitem_not_operating_inplace(self, value, set_value, indexer):
12981285
df[indexer] = set_value
12991286
tm.assert_frame_equal(view, expected)
13001287

1301-
def test_setitem_column_update_inplace(self, using_copy_on_write):
1288+
def test_setitem_column_update_inplace(self):
13021289
# https://github.com/pandas-dev/pandas/issues/47172
13031290

13041291
labels = [f"c{i}" for i in range(10)]
@@ -1308,12 +1295,8 @@ def test_setitem_column_update_inplace(self, using_copy_on_write):
13081295
with tm.raises_chained_assignment_error():
13091296
for label in df.columns:
13101297
df[label][label] = 1
1311-
if not using_copy_on_write:
1312-
# diagonal values all updated
1313-
assert np.all(values[np.arange(10), np.arange(10)] == 1)
1314-
else:
1315-
# original dataframe not updated
1316-
assert np.all(values[np.arange(10), np.arange(10)] == 0)
1298+
# original dataframe not updated
1299+
assert np.all(values[np.arange(10), np.arange(10)] == 0)
13171300

13181301
def test_setitem_column_frame_as_category(self):
13191302
# GH31581

pandas/tests/frame/indexing/test_xs.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_xs_dt_error(self, datetime_frame):
5858
):
5959
datetime_frame.xs(datetime_frame.index[0] - BDay())
6060

61-
def test_xs_other(self, float_frame, using_copy_on_write):
61+
def test_xs_other(self, float_frame):
6262
float_frame_orig = float_frame.copy()
6363
# xs get column
6464
series = float_frame.xs("A", axis=1)
@@ -68,12 +68,9 @@ def test_xs_other(self, float_frame, using_copy_on_write):
6868
# view is returned if possible
6969
series = float_frame.xs("A", axis=1)
7070
series[:] = 5
71-
if using_copy_on_write:
72-
# but with CoW the view shouldn't propagate mutations
73-
tm.assert_series_equal(float_frame["A"], float_frame_orig["A"])
74-
assert not (expected == 5).all()
75-
else:
76-
assert (expected == 5).all()
71+
# The view shouldn't propagate mutations
72+
tm.assert_series_equal(float_frame["A"], float_frame_orig["A"])
73+
assert not (expected == 5).all()
7774

7875
def test_xs_corner(self):
7976
# pathological mixed-type reordering case
@@ -363,34 +360,23 @@ def test_xs_droplevel_false(self):
363360
expected = DataFrame({"a": [1]})
364361
tm.assert_frame_equal(result, expected)
365362

366-
def test_xs_droplevel_false_view(self, using_copy_on_write):
363+
def test_xs_droplevel_false_view(self):
367364
# GH#37832
368365
df = DataFrame([[1, 2, 3]], columns=Index(["a", "b", "c"]))
369366
result = df.xs("a", axis=1, drop_level=False)
370367
# check that result still views the same data as df
371368
assert np.shares_memory(result.iloc[:, 0]._values, df.iloc[:, 0]._values)
372369

373370
df.iloc[0, 0] = 2
374-
if using_copy_on_write:
375-
# with copy on write the subset is never modified
376-
expected = DataFrame({"a": [1]})
377-
else:
378-
# modifying original df also modifies result when having a single block
379-
expected = DataFrame({"a": [2]})
371+
# The subset is never modified
372+
expected = DataFrame({"a": [1]})
380373
tm.assert_frame_equal(result, expected)
381374

382-
# with mixed dataframe, modifying the parent doesn't modify result
383-
# TODO the "split" path behaves differently here as with single block
384375
df = DataFrame([[1, 2.5, "a"]], columns=Index(["a", "b", "c"]))
385376
result = df.xs("a", axis=1, drop_level=False)
386377
df.iloc[0, 0] = 2
387-
if using_copy_on_write:
388-
# with copy on write the subset is never modified
389-
expected = DataFrame({"a": [1]})
390-
else:
391-
# FIXME: iloc does not update the array inplace using
392-
# "split" path
393-
expected = DataFrame({"a": [1]})
378+
# The subset is never modified
379+
expected = DataFrame({"a": [1]})
394380
tm.assert_frame_equal(result, expected)
395381

396382
def test_xs_list_indexer_droplevel_false(self):

pandas/tests/frame/methods/test_align.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,12 @@ def test_frame_align_aware(self):
4848
assert new1.index.tz is timezone.utc
4949
assert new2.index.tz is timezone.utc
5050

51-
def test_align_float(self, float_frame, using_copy_on_write):
51+
def test_align_float(self, float_frame):
5252
af, bf = float_frame.align(float_frame)
5353
assert af._mgr is not float_frame._mgr
5454

5555
af, bf = float_frame.align(float_frame, copy=False)
56-
if not using_copy_on_write:
57-
assert af._mgr is float_frame._mgr
58-
else:
59-
assert af._mgr is not float_frame._mgr
56+
assert af._mgr is not float_frame._mgr
6057

6158
# axis = 0
6259
other = float_frame.iloc[:-5, :3]

pandas/tests/frame/methods/test_cov_corr.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_corr_nullable_integer(self, nullable_column, other_column, method):
207207
expected = DataFrame(np.ones((2, 2)), columns=["a", "b"], index=["a", "b"])
208208
tm.assert_frame_equal(result, expected)
209209

210-
def test_corr_item_cache(self, using_copy_on_write):
210+
def test_corr_item_cache(self):
211211
# Check that corr does not lead to incorrect entries in item_cache
212212

213213
df = DataFrame({"A": range(10)})
@@ -218,15 +218,8 @@ def test_corr_item_cache(self, using_copy_on_write):
218218

219219
_ = df.corr(numeric_only=True)
220220

221-
if using_copy_on_write:
222-
ser.iloc[0] = 99
223-
assert df.loc[0, "A"] == 0
224-
else:
225-
# Check that the corr didn't break link between ser and df
226-
ser.values[0] = 99
227-
assert df.loc[0, "A"] == 99
228-
assert df["A"] is ser
229-
assert df.values[0, 0] == 99
221+
ser.iloc[0] = 99
222+
assert df.loc[0, "A"] == 0
230223

231224
@pytest.mark.parametrize("length", [2, 20, 200, 2000])
232225
def test_corr_for_constant_columns(self, length):

0 commit comments

Comments
 (0)