Skip to content

Commit f8a3747

Browse files
authored
CoW: Remove Copy-on-Write fixtures from more tests (#57367)
1 parent 5e92b37 commit f8a3747

File tree

11 files changed

+32
-121
lines changed

11 files changed

+32
-121
lines changed

pandas/tests/apply/test_frame_apply.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ def test_apply_dtype(col):
14871487
tm.assert_series_equal(result, expected)
14881488

14891489

1490-
def test_apply_mutating(using_copy_on_write):
1490+
def test_apply_mutating():
14911491
# GH#35462 case where applied func pins a new BlockManager to a row
14921492
df = DataFrame({"a": range(100), "b": range(100, 200)})
14931493
df_orig = df.copy()
@@ -1504,11 +1504,7 @@ def func(row):
15041504
result = df.apply(func, axis=1)
15051505

15061506
tm.assert_frame_equal(result, expected)
1507-
if using_copy_on_write:
1508-
# INFO(CoW) With copy on write, mutating a viewing row doesn't mutate the parent
1509-
tm.assert_frame_equal(df, df_orig)
1510-
else:
1511-
tm.assert_frame_equal(df, result)
1507+
tm.assert_frame_equal(df, df_orig)
15121508

15131509

15141510
def test_apply_empty_list_reduce():

pandas/tests/computation/test_eval.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,7 @@ def test_eval_no_support_column_name(request, column):
19641964
tm.assert_frame_equal(result, expected)
19651965

19661966

1967-
def test_set_inplace(using_copy_on_write):
1967+
def test_set_inplace():
19681968
# https://github.com/pandas-dev/pandas/issues/47449
19691969
# Ensure we don't only update the DataFrame inplace, but also the actual
19701970
# column values, such that references to this column also get updated
@@ -1974,13 +1974,9 @@ def test_set_inplace(using_copy_on_write):
19741974
df.eval("A = B + C", inplace=True)
19751975
expected = DataFrame({"A": [11, 13, 15], "B": [4, 5, 6], "C": [7, 8, 9]})
19761976
tm.assert_frame_equal(df, expected)
1977-
if not using_copy_on_write:
1978-
tm.assert_series_equal(ser, expected["A"])
1979-
tm.assert_series_equal(result_view["A"], expected["A"])
1980-
else:
1981-
expected = Series([1, 2, 3], name="A")
1982-
tm.assert_series_equal(ser, expected)
1983-
tm.assert_series_equal(result_view["A"], expected)
1977+
expected = Series([1, 2, 3], name="A")
1978+
tm.assert_series_equal(ser, expected)
1979+
tm.assert_series_equal(result_view["A"], expected)
19841980

19851981

19861982
class TestValidate:

pandas/tests/extension/base/setitem.py

-7
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,6 @@ def test_setitem_series(self, data, full_indexer):
398398
def test_setitem_frame_2d_values(self, data):
399399
# GH#44514
400400
df = pd.DataFrame({"A": data})
401-
using_copy_on_write = pd.options.mode.copy_on_write
402-
403-
blk_data = df._mgr.arrays[0]
404-
405401
orig = df.copy()
406402

407403
df.iloc[:] = df.copy()
@@ -412,9 +408,6 @@ def test_setitem_frame_2d_values(self, data):
412408

413409
df.iloc[:] = df.values
414410
tm.assert_frame_equal(df, orig)
415-
if not using_copy_on_write:
416-
# GH#33457 Check that this setting occurred in-place
417-
assert df._mgr.arrays[0] is blk_data
418411

419412
df.iloc[:-1] = df.values[:-1]
420413
tm.assert_frame_equal(df, orig)

pandas/tests/extension/conftest.py

-8
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,3 @@ def invalid_scalar(data):
212212
If the array can hold any item (i.e. object dtype), then use pytest.skip.
213213
"""
214214
return object.__new__(object)
215-
216-
217-
@pytest.fixture
218-
def using_copy_on_write() -> bool:
219-
"""
220-
Fixture to check if Copy-on-Write is enabled.
221-
"""
222-
return True

pandas/tests/extension/decimal/test_decimal.py

+5-18
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,6 @@ def test_fillna_series_method(self, data_missing, fillna_method):
245245
):
246246
super().test_fillna_series_method(data_missing, fillna_method)
247247

248-
def test_fillna_copy_frame(self, data_missing, using_copy_on_write):
249-
warn = DeprecationWarning if not using_copy_on_write else None
250-
msg = "ExtensionArray.fillna added a 'copy' keyword"
251-
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
252-
super().test_fillna_copy_frame(data_missing)
253-
254-
def test_fillna_copy_series(self, data_missing, using_copy_on_write):
255-
warn = DeprecationWarning if not using_copy_on_write else None
256-
msg = "ExtensionArray.fillna added a 'copy' keyword"
257-
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
258-
super().test_fillna_copy_series(data_missing)
259-
260248
@pytest.mark.parametrize("dropna", [True, False])
261249
def test_value_counts(self, all_data, dropna):
262250
all_data = all_data[:10]
@@ -554,12 +542,11 @@ def test_to_numpy_keyword():
554542
tm.assert_numpy_array_equal(result, expected)
555543

556544

557-
def test_array_copy_on_write(using_copy_on_write):
545+
def test_array_copy_on_write():
558546
df = pd.DataFrame({"a": [decimal.Decimal(2), decimal.Decimal(3)]}, dtype="object")
559547
df2 = df.astype(DecimalDtype())
560548
df.iloc[0, 0] = 0
561-
if using_copy_on_write:
562-
expected = pd.DataFrame(
563-
{"a": [decimal.Decimal(2), decimal.Decimal(3)]}, dtype=DecimalDtype()
564-
)
565-
tm.assert_equal(df2.values, expected.values)
549+
expected = pd.DataFrame(
550+
{"a": [decimal.Decimal(2), decimal.Decimal(3)]}, dtype=DecimalDtype()
551+
)
552+
tm.assert_equal(df2.values, expected.values)

pandas/tests/extension/json/test_json.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,8 @@ def test_equals(self, data, na_value, as_series):
217217
def test_fillna_copy_frame(self, data_missing):
218218
super().test_fillna_copy_frame(data_missing)
219219

220-
def test_equals_same_data_different_object(
221-
self, data, using_copy_on_write, request
222-
):
223-
if using_copy_on_write:
224-
mark = pytest.mark.xfail(reason="Fails with CoW")
225-
request.applymarker(mark)
220+
@pytest.mark.xfail(reason="Fails with CoW")
221+
def test_equals_same_data_different_object(self, data, request):
226222
super().test_equals_same_data_different_object(data)
227223

228224
@pytest.mark.xfail(reason="failing on np.array(self, dtype=str)")

pandas/tests/extension/test_sparse.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -277,32 +277,25 @@ def test_fillna_frame(self, data_missing):
277277

278278
_combine_le_expected_dtype = "Sparse[bool]"
279279

280-
def test_fillna_copy_frame(self, data_missing, using_copy_on_write):
280+
def test_fillna_copy_frame(self, data_missing):
281281
arr = data_missing.take([1, 1])
282282
df = pd.DataFrame({"A": arr}, copy=False)
283283

284284
filled_val = df.iloc[0, 0]
285285
result = df.fillna(filled_val)
286286

287287
if hasattr(df._mgr, "blocks"):
288-
if using_copy_on_write:
289-
assert df.values.base is result.values.base
290-
else:
291-
assert df.values.base is not result.values.base
288+
assert df.values.base is result.values.base
292289
assert df.A._values.to_dense() is arr.to_dense()
293290

294-
def test_fillna_copy_series(self, data_missing, using_copy_on_write):
291+
def test_fillna_copy_series(self, data_missing):
295292
arr = data_missing.take([1, 1])
296293
ser = pd.Series(arr, copy=False)
297294

298295
filled_val = ser[0]
299296
result = ser.fillna(filled_val)
300297

301-
if using_copy_on_write:
302-
assert ser._values is result._values
303-
304-
else:
305-
assert ser._values is not result._values
298+
assert ser._values is result._values
306299
assert ser._values.to_dense() is arr.to_dense()
307300

308301
@pytest.mark.xfail(reason="Not Applicable")

pandas/tests/groupby/test_raises.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def test_groupby_raises_timedelta(func):
363363

364364
@pytest.mark.parametrize("how", ["method", "agg", "transform"])
365365
def test_groupby_raises_category(
366-
how, by, groupby_series, groupby_func, using_copy_on_write, df_with_cat_col
366+
how, by, groupby_series, groupby_func, df_with_cat_col
367367
):
368368
# GH#50749
369369
df = df_with_cat_col
@@ -416,13 +416,7 @@ def test_groupby_raises_category(
416416
r"unsupported operand type\(s\) for -: 'Categorical' and 'Categorical'",
417417
),
418418
"ffill": (None, ""),
419-
"fillna": (
420-
TypeError,
421-
r"Cannot setitem on a Categorical with a new category \(0\), "
422-
"set the categories first",
423-
)
424-
if not using_copy_on_write
425-
else (None, ""), # no-op with CoW
419+
"fillna": (None, ""), # no-op with CoW
426420
"first": (None, ""),
427421
"idxmax": (None, ""),
428422
"idxmin": (None, ""),
@@ -555,7 +549,6 @@ def test_groupby_raises_category_on_category(
555549
groupby_series,
556550
groupby_func,
557551
observed,
558-
using_copy_on_write,
559552
df_with_cat_col,
560553
):
561554
# GH#50749
@@ -616,13 +609,7 @@ def test_groupby_raises_category_on_category(
616609
),
617610
"diff": (TypeError, "unsupported operand type"),
618611
"ffill": (None, ""),
619-
"fillna": (
620-
TypeError,
621-
r"Cannot setitem on a Categorical with a new category \(0\), "
622-
"set the categories first",
623-
)
624-
if not using_copy_on_write
625-
else (None, ""), # no-op with CoW
612+
"fillna": (None, ""), # no-op with CoW
626613
"first": (None, ""),
627614
"idxmax": (ValueError, "empty group due to unobserved categories")
628615
if empty_groups

pandas/tests/reshape/concat/test_concat.py

+8-21
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,15 @@ def test_append_concat(self):
4343
assert isinstance(result.index, PeriodIndex)
4444
assert result.index[0] == s1.index[0]
4545

46-
def test_concat_copy(self, using_copy_on_write):
46+
def test_concat_copy(self):
4747
df = DataFrame(np.random.default_rng(2).standard_normal((4, 3)))
4848
df2 = DataFrame(np.random.default_rng(2).integers(0, 10, size=4).reshape(4, 1))
4949
df3 = DataFrame({5: "foo"}, index=range(4))
5050

5151
# These are actual copies.
5252
result = concat([df, df2, df3], axis=1, copy=True)
53-
54-
if not using_copy_on_write:
55-
for arr in result._mgr.arrays:
56-
assert not any(
57-
np.shares_memory(arr, y)
58-
for x in [df, df2, df3]
59-
for y in x._mgr.arrays
60-
)
61-
else:
62-
for arr in result._mgr.arrays:
63-
assert arr.base is not None
53+
for arr in result._mgr.arrays:
54+
assert arr.base is not None
6455

6556
# These are the same.
6657
result = concat([df, df2, df3], axis=1, copy=False)
@@ -78,15 +69,11 @@ def test_concat_copy(self, using_copy_on_write):
7869
result = concat([df, df2, df3, df4], axis=1, copy=False)
7970
for arr in result._mgr.arrays:
8071
if arr.dtype.kind == "f":
81-
if using_copy_on_write:
82-
# this is a view on some array in either df or df4
83-
assert any(
84-
np.shares_memory(arr, other)
85-
for other in df._mgr.arrays + df4._mgr.arrays
86-
)
87-
else:
88-
# the block was consolidated, so we got a copy anyway
89-
assert arr.base is None
72+
# this is a view on some array in either df or df4
73+
assert any(
74+
np.shares_memory(arr, other)
75+
for other in df._mgr.arrays + df4._mgr.arrays
76+
)
9077
elif arr.dtype.kind in ["i", "u"]:
9178
assert arr.base is df2._mgr.arrays[0].base
9279
elif arr.dtype == object:

pandas/tests/reshape/concat/test_dataframe.py

-13
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,6 @@ def test_concat_duplicates_in_index_with_keys(self):
192192
tm.assert_frame_equal(result, expected)
193193
tm.assert_index_equal(result.index.levels[1], Index([1, 3], name="date"))
194194

195-
@pytest.mark.parametrize("ignore_index", [True, False])
196-
@pytest.mark.parametrize("order", ["C", "F"])
197-
def test_concat_copies(self, axis, order, ignore_index, using_copy_on_write):
198-
# based on asv ConcatDataFrames
199-
df = DataFrame(np.zeros((10, 5), dtype=np.float32, order=order))
200-
201-
res = concat([df] * 5, axis=axis, ignore_index=ignore_index, copy=True)
202-
203-
if not using_copy_on_write:
204-
for arr in res._iter_column_arrays():
205-
for arr2 in df._iter_column_arrays():
206-
assert not np.shares_memory(arr, arr2)
207-
208195
def test_outer_sort_columns(self):
209196
# GH#47127
210197
df1 = DataFrame({"A": [0], "B": [1], 0: 1})

pandas/tests/reshape/concat/test_index.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,20 @@ def test_concat_rename_index(self):
100100
tm.assert_frame_equal(result, exp)
101101
assert result.index.names == exp.index.names
102102

103-
def test_concat_copy_index_series(self, axis, using_copy_on_write):
103+
def test_concat_copy_index_series(self, axis):
104104
# GH 29879
105105
ser = Series([1, 2])
106106
comb = concat([ser, ser], axis=axis, copy=True)
107-
if not using_copy_on_write or axis in [0, "index"]:
107+
if axis in [0, "index"]:
108108
assert comb.index is not ser.index
109109
else:
110110
assert comb.index is ser.index
111111

112-
def test_concat_copy_index_frame(self, axis, using_copy_on_write):
112+
def test_concat_copy_index_frame(self, axis):
113113
# GH 29879
114114
df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"])
115115
comb = concat([df, df], axis=axis, copy=True)
116-
if not using_copy_on_write:
117-
assert not comb.index.is_(df.index)
118-
assert not comb.columns.is_(df.columns)
119-
elif axis in [0, "index"]:
116+
if axis in [0, "index"]:
120117
assert not comb.index.is_(df.index)
121118
assert comb.columns.is_(df.columns)
122119
elif axis in [1, "columns"]:

0 commit comments

Comments
 (0)