Skip to content

Commit 17aa2ba

Browse files
authored
CoW: Remove cow branches from more tests (#57270)
1 parent 8baee5d commit 17aa2ba

16 files changed

+102
-286
lines changed

pandas/tests/indexing/multiindex/test_chaining_and_caching.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_detect_chained_assignment():
3131
zed["eyes"]["right"].fillna(value=555, inplace=True)
3232

3333

34-
def test_cache_updating(using_copy_on_write):
34+
def test_cache_updating():
3535
# 5216
3636
# make sure that we don't try to set a dead cache
3737
a = np.random.default_rng(2).random((10, 3))
@@ -47,11 +47,7 @@ def test_cache_updating(using_copy_on_write):
4747
with tm.raises_chained_assignment_error():
4848
df.loc[0]["z"].iloc[0] = 1.0
4949

50-
if using_copy_on_write:
51-
assert df.loc[(0, 0), "z"] == df_original.loc[0, "z"]
52-
else:
53-
result = df.loc[(0, 0), "z"]
54-
assert result == 1
50+
assert df.loc[(0, 0), "z"] == df_original.loc[0, "z"]
5551

5652
# correct setting
5753
df.loc[(0, 0), "z"] = 2

pandas/tests/indexing/multiindex/test_partial.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ def test_getitem_partial_column_select(self):
119119
def test_partial_set(
120120
self,
121121
multiindex_year_month_day_dataframe_random_data,
122-
using_copy_on_write,
123122
):
124123
# GH #397
125124
ymd = multiindex_year_month_day_dataframe_random_data
@@ -129,13 +128,9 @@ def test_partial_set(
129128
exp.iloc[65:85] = 0
130129
tm.assert_frame_equal(df, exp)
131130

132-
if using_copy_on_write:
133-
with tm.raises_chained_assignment_error():
134-
df["A"].loc[2000, 4] = 1
135-
df.loc[(2000, 4), "A"] = 1
136-
else:
137-
with tm.raises_chained_assignment_error():
138-
df["A"].loc[2000, 4] = 1
131+
with tm.raises_chained_assignment_error():
132+
df["A"].loc[2000, 4] = 1
133+
df.loc[(2000, 4), "A"] = 1
139134
exp.iloc[65:85, 0] = 1
140135
tm.assert_frame_equal(df, exp)
141136

@@ -146,10 +141,7 @@ def test_partial_set(
146141
# this works...for now
147142
with tm.raises_chained_assignment_error():
148143
df["A"].iloc[14] = 5
149-
if using_copy_on_write:
150-
assert df["A"].iloc[14] == exp["A"].iloc[14]
151-
else:
152-
assert df["A"].iloc[14] == 5
144+
assert df["A"].iloc[14] == exp["A"].iloc[14]
153145

154146
@pytest.mark.parametrize("dtype", [int, float])
155147
def test_getitem_intkey_leading_level(

pandas/tests/indexing/multiindex/test_setitem.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_multiindex_assignment(self):
194194
df.loc[4, "d"] = arr
195195
tm.assert_series_equal(df.loc[4, "d"], Series(arr, index=[8, 10], name="d"))
196196

197-
def test_multiindex_assignment_single_dtype(self, using_copy_on_write):
197+
def test_multiindex_assignment_single_dtype(self):
198198
# GH3777 part 2b
199199
# single dtype
200200
arr = np.array([0.0, 1.0])
@@ -205,7 +205,6 @@ def test_multiindex_assignment_single_dtype(self, using_copy_on_write):
205205
index=[[4, 4, 8], [8, 10, 12]],
206206
dtype=np.int64,
207207
)
208-
view = df["c"].iloc[:2].values
209208

210209
# arr can be losslessly cast to int, so this setitem is inplace
211210
# 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):
215214
result = df.loc[4, "c"]
216215
tm.assert_series_equal(result, exp)
217216

218-
# extra check for inplace-ness
219-
if not using_copy_on_write:
220-
tm.assert_numpy_array_equal(view, exp.values)
221-
222217
# arr + 0.5 cannot be cast losslessly to int, so we upcast
223218
with tm.assert_produces_warning(
224219
FutureWarning, match="item of incompatible dtype"
@@ -412,9 +407,7 @@ def test_setitem_change_dtype(self, multiindex_dataframe_random_data):
412407
reindexed = dft.reindex(columns=[("foo", "two")])
413408
tm.assert_series_equal(reindexed["foo", "two"], s > s.median())
414409

415-
def test_set_column_scalar_with_loc(
416-
self, multiindex_dataframe_random_data, using_copy_on_write
417-
):
410+
def test_set_column_scalar_with_loc(self, multiindex_dataframe_random_data):
418411
frame = multiindex_dataframe_random_data
419412
subset = frame.index[[1, 4, 5]]
420413

@@ -424,11 +417,8 @@ def test_set_column_scalar_with_loc(
424417
frame_original = frame.copy()
425418
col = frame["B"]
426419
col[subset] = 97
427-
if using_copy_on_write:
428-
# chained setitem doesn't work with CoW
429-
tm.assert_frame_equal(frame, frame_original)
430-
else:
431-
assert (frame.loc[subset, "B"] == 97).all()
420+
# chained setitem doesn't work with CoW
421+
tm.assert_frame_equal(frame, frame_original)
432422

433423
def test_nonunique_assignment_1750(self):
434424
df = DataFrame(
@@ -505,19 +495,13 @@ def test_setitem_enlargement_keep_index_names(self):
505495
tm.assert_frame_equal(df, expected)
506496

507497

508-
def test_frame_setitem_view_direct(
509-
multiindex_dataframe_random_data, using_copy_on_write
510-
):
498+
def test_frame_setitem_view_direct(multiindex_dataframe_random_data):
511499
# this works because we are modifying the underlying array
512500
# really a no-no
513501
df = multiindex_dataframe_random_data.T
514-
if using_copy_on_write:
515-
with pytest.raises(ValueError, match="read-only"):
516-
df["foo"].values[:] = 0
517-
assert (df["foo"].values != 0).all()
518-
else:
502+
with pytest.raises(ValueError, match="read-only"):
519503
df["foo"].values[:] = 0
520-
assert (df["foo"].values == 0).all()
504+
assert (df["foo"].values != 0).all()
521505

522506

523507
def test_frame_setitem_copy_raises(multiindex_dataframe_random_data):

pandas/tests/indexing/test_chaining_and_caching.py

+20-61
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def random_text(nobs=100):
2727

2828

2929
class TestCaching:
30-
def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write):
30+
def test_slice_consolidate_invalidate_item_cache(self):
3131
# this is chained assignment, but will 'work'
3232
with option_context("chained_assignment", None):
3333
# #3970
@@ -61,7 +61,7 @@ def test_setitem_cache_updating(self, do_ref):
6161
assert df.loc[0, "c"] == 0.0
6262
assert df.loc[7, "c"] == 1.0
6363

64-
def test_setitem_cache_updating_slices(self, using_copy_on_write):
64+
def test_setitem_cache_updating_slices(self):
6565
# GH 7084
6666
# not updating cache on series setting with slices
6767
expected = DataFrame(
@@ -85,15 +85,11 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
8585
out_original = out.copy()
8686
for ix, row in df.iterrows():
8787
v = out[row["C"]][six:eix] + row["D"]
88-
with tm.raises_chained_assignment_error((ix == 0) or using_copy_on_write):
88+
with tm.raises_chained_assignment_error():
8989
out[row["C"]][six:eix] = v
9090

91-
if not using_copy_on_write:
92-
tm.assert_frame_equal(out, expected)
93-
tm.assert_series_equal(out["A"], expected["A"])
94-
else:
95-
tm.assert_frame_equal(out, out_original)
96-
tm.assert_series_equal(out["A"], out_original["A"])
91+
tm.assert_frame_equal(out, out_original)
92+
tm.assert_series_equal(out["A"], out_original["A"])
9793

9894
out = DataFrame({"A": [0, 0, 0]}, index=date_range("5/7/2014", "5/9/2014"))
9995
for ix, row in df.iterrows():
@@ -102,7 +98,7 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
10298
tm.assert_frame_equal(out, expected)
10399
tm.assert_series_equal(out["A"], expected["A"])
104100

105-
def test_altering_series_clears_parent_cache(self, using_copy_on_write):
101+
def test_altering_series_clears_parent_cache(self):
106102
# GH #33675
107103
df = DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["A", "B"])
108104
ser = df["A"]
@@ -116,49 +112,36 @@ def test_altering_series_clears_parent_cache(self, using_copy_on_write):
116112

117113

118114
class TestChaining:
119-
def test_setitem_chained_setfault(self, using_copy_on_write):
115+
def test_setitem_chained_setfault(self):
120116
# GH6026
121117
data = ["right", "left", "left", "left", "right", "left", "timeout"]
122-
mdata = ["right", "left", "left", "left", "right", "left", "none"]
123118

124119
df = DataFrame({"response": np.array(data)})
125120
mask = df.response == "timeout"
126121
with tm.raises_chained_assignment_error():
127122
df.response[mask] = "none"
128-
if using_copy_on_write:
129-
tm.assert_frame_equal(df, DataFrame({"response": data}))
130-
else:
131-
tm.assert_frame_equal(df, DataFrame({"response": mdata}))
123+
tm.assert_frame_equal(df, DataFrame({"response": data}))
132124

133125
recarray = np.rec.fromarrays([data], names=["response"])
134126
df = DataFrame(recarray)
135127
mask = df.response == "timeout"
136128
with tm.raises_chained_assignment_error():
137129
df.response[mask] = "none"
138-
if using_copy_on_write:
139-
tm.assert_frame_equal(df, DataFrame({"response": data}))
140-
else:
141-
tm.assert_frame_equal(df, DataFrame({"response": mdata}))
130+
tm.assert_frame_equal(df, DataFrame({"response": data}))
142131

143132
df = DataFrame({"response": data, "response1": data})
144133
df_original = df.copy()
145134
mask = df.response == "timeout"
146135
with tm.raises_chained_assignment_error():
147136
df.response[mask] = "none"
148-
if using_copy_on_write:
149-
tm.assert_frame_equal(df, df_original)
150-
else:
151-
tm.assert_frame_equal(df, DataFrame({"response": mdata, "response1": data}))
137+
tm.assert_frame_equal(df, df_original)
152138

153139
# GH 6056
154140
expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]})
155141
df = DataFrame({"A": np.array(["foo", "bar", "bah", "foo", "bar"])})
156142
with tm.raises_chained_assignment_error():
157143
df["A"].iloc[0] = np.nan
158-
if using_copy_on_write:
159-
expected = DataFrame({"A": ["foo", "bar", "bah", "foo", "bar"]})
160-
else:
161-
expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]})
144+
expected = DataFrame({"A": ["foo", "bar", "bah", "foo", "bar"]})
162145
result = df.head()
163146
tm.assert_frame_equal(result, expected)
164147

@@ -169,10 +152,9 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
169152
tm.assert_frame_equal(result, expected)
170153

171154
@pytest.mark.arm_slow
172-
def test_detect_chained_assignment(self, using_copy_on_write):
155+
def test_detect_chained_assignment(self):
173156
with option_context("chained_assignment", "raise"):
174157
# work with the chain
175-
expected = DataFrame([[-5, 1], [-6, 3]], columns=list("AB"))
176158
df = DataFrame(
177159
np.arange(4).reshape(2, 2), columns=list("AB"), dtype="int64"
178160
)
@@ -182,10 +164,7 @@ def test_detect_chained_assignment(self, using_copy_on_write):
182164
df["A"][0] = -5
183165
with tm.raises_chained_assignment_error():
184166
df["A"][1] = -6
185-
if using_copy_on_write:
186-
tm.assert_frame_equal(df, df_original)
187-
else:
188-
tm.assert_frame_equal(df, expected)
167+
tm.assert_frame_equal(df, df_original)
189168

190169
@pytest.mark.arm_slow
191170
def test_detect_chained_assignment_raises(self):
@@ -340,9 +319,7 @@ def test_detect_chained_assignment_warnings_errors(self):
340319
df.loc[0]["A"] = 111
341320

342321
@pytest.mark.parametrize("rhs", [3, DataFrame({0: [1, 2, 3, 4]})])
343-
def test_detect_chained_assignment_warning_stacklevel(
344-
self, rhs, using_copy_on_write
345-
):
322+
def test_detect_chained_assignment_warning_stacklevel(self, rhs):
346323
# GH#42570
347324
df = DataFrame(np.arange(25).reshape(5, 5))
348325
df_original = df.copy()
@@ -379,7 +356,7 @@ def test_cache_updating(self):
379356
assert "Hello Friend" in df["A"].index
380357
assert "Hello Friend" in df["B"].index
381358

382-
def test_cache_updating2(self, using_copy_on_write):
359+
def test_cache_updating2(self):
383360
# 10264
384361
df = DataFrame(
385362
np.zeros((5, 5), dtype="int64"),
@@ -388,26 +365,11 @@ def test_cache_updating2(self, using_copy_on_write):
388365
)
389366
df["f"] = 0
390367
df_orig = df.copy()
391-
if using_copy_on_write:
392-
with pytest.raises(ValueError, match="read-only"):
393-
df.f.values[3] = 1
394-
tm.assert_frame_equal(df, df_orig)
395-
return
396-
397-
df.f.values[3] = 1
398-
399-
df.f.values[3] = 2
400-
expected = DataFrame(
401-
np.zeros((5, 6), dtype="int64"),
402-
columns=["a", "b", "c", "d", "e", "f"],
403-
index=range(5),
404-
)
405-
expected.at[3, "f"] = 2
406-
tm.assert_frame_equal(df, expected)
407-
expected = Series([0, 0, 0, 2, 0], name="f")
408-
tm.assert_series_equal(df.f, expected)
368+
with pytest.raises(ValueError, match="read-only"):
369+
df.f.values[3] = 1
370+
tm.assert_frame_equal(df, df_orig)
409371

410-
def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
372+
def test_iloc_setitem_chained_assignment(self):
411373
# GH#3970
412374
with option_context("chained_assignment", None):
413375
df = DataFrame({"aa": range(5), "bb": [2.2] * 5})
@@ -424,10 +386,7 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
424386
with tm.raises_chained_assignment_error():
425387
df["bb"].iloc[0] = 0.15
426388

427-
if not using_copy_on_write:
428-
assert df["bb"].iloc[0] == 0.15
429-
else:
430-
assert df["bb"].iloc[0] == 2.2
389+
assert df["bb"].iloc[0] == 2.2
431390

432391
def test_getitem_loc_assignment_slice_state(self):
433392
# GH 13569

pandas/tests/indexing/test_iat.py

-18
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,3 @@ def test_iat_getitem_series_with_period_index():
2828
expected = ser[index[0]]
2929
result = ser.iat[0]
3030
assert expected == result
31-
32-
33-
def test_iat_setitem_item_cache_cleared(indexer_ial, using_copy_on_write):
34-
# GH#45684
35-
data = {"x": np.arange(8, dtype=np.int64), "y": np.int64(0)}
36-
df = DataFrame(data).copy()
37-
ser = df["y"]
38-
39-
# previously this iat setting would split the block and fail to clear
40-
# the item_cache.
41-
indexer_ial(df)[7, 0] = 9999
42-
43-
indexer_ial(df)[7, 1] = 1234
44-
45-
assert df.iat[7, 1] == 1234
46-
if not using_copy_on_write:
47-
assert ser.iloc[-1] == 1234
48-
assert df.iloc[-1, -1] == 1234

0 commit comments

Comments
 (0)