Skip to content

Commit 5a5a1d5

Browse files
fix more tests
1 parent e55b985 commit 5a5a1d5

22 files changed

+135
-57
lines changed

pandas/core/groupby/grouper.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
import numpy as np
1414

15-
from pandas._config import using_copy_on_write
15+
from pandas._config import (
16+
using_copy_on_write,
17+
warn_copy_on_write,
18+
)
1619

1720
from pandas._libs import lib
1821
from pandas._libs.tslibs import OutOfBoundsDatetime
@@ -966,7 +969,7 @@ def is_in_axis(key) -> bool:
966969
def is_in_obj(gpr) -> bool:
967970
if not hasattr(gpr, "name"):
968971
return False
969-
if using_copy_on_write():
972+
if using_copy_on_write or warn_copy_on_write():
970973
# For the CoW case, we check the references to determine if the
971974
# series is part of the object
972975
try:

pandas/tests/apply/test_frame_apply.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ def test_apply_dtype(col):
14531453
tm.assert_series_equal(result, expected)
14541454

14551455

1456-
def test_apply_mutating(using_array_manager, using_copy_on_write):
1456+
def test_apply_mutating(using_array_manager, using_copy_on_write, warn_copy_on_write):
14571457
# GH#35462 case where applied func pins a new BlockManager to a row
14581458
df = DataFrame({"a": range(100), "b": range(100, 200)})
14591459
df_orig = df.copy()
@@ -1467,7 +1467,8 @@ def func(row):
14671467
expected = df.copy()
14681468
expected["a"] += 1
14691469

1470-
result = df.apply(func, axis=1)
1470+
with tm.assert_cow_warning(warn_copy_on_write):
1471+
result = df.apply(func, axis=1)
14711472

14721473
tm.assert_frame_equal(result, expected)
14731474
if using_copy_on_write or using_array_manager:

pandas/tests/extension/base/setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def test_setitem_frame_2d_values(self, data):
405405
df.iloc[:] = df
406406
tm.assert_frame_equal(df, orig)
407407

408-
df.iloc[:-1] = df.iloc[:-1]
408+
df.iloc[:-1] = df.iloc[:-1].copy()
409409
tm.assert_frame_equal(df, orig)
410410

411411
df.iloc[:] = df.values

pandas/tests/generic/test_duplicate_labels.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def test_preserve_getitem(self):
9090
assert df.loc[[0]].flags.allows_duplicate_labels is False
9191
assert df.loc[0, ["A"]].flags.allows_duplicate_labels is False
9292

93-
def test_ndframe_getitem_caching_issue(self, request, using_copy_on_write):
94-
if not using_copy_on_write:
93+
def test_ndframe_getitem_caching_issue(
94+
self, request, using_copy_on_write, warn_copy_on_write
95+
):
96+
if not (using_copy_on_write or warn_copy_on_write):
9597
request.applymarker(pytest.mark.xfail(reason="Unclear behavior."))
9698
# NDFrame.__getitem__ will cache the first df['A']. May need to
9799
# invalidate that cache? Update the cached entries?

pandas/tests/groupby/test_groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def test_repr():
4141
assert result == expected
4242

4343

44-
def test_groupby_std_datetimelike():
44+
# TODO(CoW-warn) this should NOT warn
45+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
46+
def test_groupby_std_datetimelike(warn_copy_on_write):
4547
# GH#48481
4648
tdi = pd.timedelta_range("1 Day", periods=10000)
4749
ser = Series(tdi)

pandas/tests/indexes/period/test_partial_slicing.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313

1414
class TestPeriodIndex:
15-
def test_getitem_periodindex_duplicates_string_slice(self, using_copy_on_write):
15+
def test_getitem_periodindex_duplicates_string_slice(
16+
self, using_copy_on_write, warn_copy_on_write
17+
):
1618
# monotonic
1719
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="Y-JUN")
1820
ts = Series(np.random.default_rng(2).standard_normal(len(idx)), index=idx)
@@ -21,7 +23,8 @@ def test_getitem_periodindex_duplicates_string_slice(self, using_copy_on_write):
2123
result = ts["2007"]
2224
expected = ts[1:3]
2325
tm.assert_series_equal(result, expected)
24-
result[:] = 1
26+
with tm.assert_cow_warning(warn_copy_on_write):
27+
result[:] = 1
2528
if using_copy_on_write:
2629
tm.assert_series_equal(ts, original)
2730
else:

pandas/tests/indexing/multiindex/test_chaining_and_caching.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_detect_chained_assignment(using_copy_on_write, warn_copy_on_write):
4242

4343

4444
@td.skip_array_manager_invalid_test # with ArrayManager df.loc[0] is not a view
45-
def test_cache_updating(using_copy_on_write):
45+
def test_cache_updating(using_copy_on_write, warn_copy_on_write):
4646
# 5216
4747
# make sure that we don't try to set a dead cache
4848
a = np.random.default_rng(2).random((10, 3))
@@ -59,7 +59,8 @@ def test_cache_updating(using_copy_on_write):
5959
df.loc[0]["z"].iloc[0] = 1.0
6060
assert df.loc[(0, 0), "z"] == df_original.loc[0, "z"]
6161
else:
62-
df.loc[0]["z"].iloc[0] = 1.0
62+
with tm.assert_cow_warning(warn_copy_on_write):
63+
df.loc[0]["z"].iloc[0] = 1.0
6364
result = df.loc[(0, 0), "z"]
6465
assert result == 1
6566

pandas/tests/indexing/multiindex/test_partial.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def test_getitem_partial_column_select(self):
122122
# exp.loc[2000, 4].values[:] select multiple columns -> .values is not a view
123123
@td.skip_array_manager_invalid_test
124124
def test_partial_set(
125-
self, multiindex_year_month_day_dataframe_random_data, using_copy_on_write
125+
self,
126+
multiindex_year_month_day_dataframe_random_data,
127+
using_copy_on_write,
128+
warn_copy_on_write,
126129
):
127130
# GH #397
128131
ymd = multiindex_year_month_day_dataframe_random_data
@@ -137,7 +140,8 @@ def test_partial_set(
137140
df["A"].loc[2000, 4] = 1
138141
df.loc[(2000, 4), "A"] = 1
139142
else:
140-
df["A"].loc[2000, 4] = 1
143+
with tm.assert_cow_warning(warn_copy_on_write):
144+
df["A"].loc[2000, 4] = 1
141145
exp.iloc[65:85, 0] = 1
142146
tm.assert_frame_equal(df, exp)
143147

@@ -151,7 +155,8 @@ def test_partial_set(
151155
df["A"].iloc[14] = 5
152156
df["A"].iloc[14] == exp["A"].iloc[14]
153157
else:
154-
df["A"].iloc[14] = 5
158+
with tm.assert_cow_warning(warn_copy_on_write):
159+
df["A"].iloc[14] = 5
155160
assert df["A"].iloc[14] == 5
156161

157162
@pytest.mark.parametrize("dtype", [int, float])

pandas/tests/indexing/multiindex/test_setitem.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def test_loc_getitem_tuple_plus_columns(
390390
expected = df.loc[2000, 1, 6][["A", "B", "C"]]
391391
tm.assert_series_equal(result, expected)
392392

393+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
393394
def test_loc_getitem_setitem_slice_integers(self, frame_or_series):
394395
index = MultiIndex(
395396
levels=[[0, 1, 2], [0, 2]], codes=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]]
@@ -421,7 +422,7 @@ def test_setitem_change_dtype(self, multiindex_dataframe_random_data):
421422
tm.assert_series_equal(reindexed["foo", "two"], s > s.median())
422423

423424
def test_set_column_scalar_with_loc(
424-
self, multiindex_dataframe_random_data, using_copy_on_write
425+
self, multiindex_dataframe_random_data, using_copy_on_write, warn_copy_on_write
425426
):
426427
frame = multiindex_dataframe_random_data
427428
subset = frame.index[[1, 4, 5]]
@@ -431,7 +432,8 @@ def test_set_column_scalar_with_loc(
431432

432433
frame_original = frame.copy()
433434
col = frame["B"]
434-
col[subset] = 97
435+
with tm.assert_cow_warning(warn_copy_on_write):
436+
col[subset] = 97
435437
if using_copy_on_write:
436438
# chained setitem doesn't work with CoW
437439
tm.assert_frame_equal(frame, frame_original)

pandas/tests/indexing/multiindex/test_slice.py

+1
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ def test_int_series_slicing(self, multiindex_year_month_day_dataframe_random_dat
739739
expected = s.reindex(s.index[5:])
740740
tm.assert_series_equal(result, expected)
741741

742+
s = ymd["A"].copy()
742743
exp = ymd["A"].copy()
743744
s[5:] = 0
744745
exp.iloc[5:] = 0

pandas/tests/indexing/test_chaining_and_caching.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def random_text(nobs=100):
3232

3333

3434
class TestCaching:
35-
def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write):
35+
def test_slice_consolidate_invalidate_item_cache(
36+
self, using_copy_on_write, warn_copy_on_write
37+
):
3638
# this is chained assignment, but will 'work'
3739
with option_context("chained_assignment", None):
3840
# #3970
@@ -49,7 +51,9 @@ def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write):
4951
with tm.raises_chained_assignment_error():
5052
df["bb"].iloc[0] = 0.17
5153
else:
52-
df["bb"].iloc[0] = 0.17
54+
# TODO(CoW-warn) custom warning message
55+
with tm.assert_cow_warning(warn_copy_on_write):
56+
df["bb"].iloc[0] = 0.17
5357
df._clear_item_cache()
5458
if not using_copy_on_write:
5559
tm.assert_almost_equal(df["bb"][0], 0.17)
@@ -74,7 +78,9 @@ def test_setitem_cache_updating(self, do_ref):
7478
assert df.loc[0, "c"] == 0.0
7579
assert df.loc[7, "c"] == 1.0
7680

77-
def test_setitem_cache_updating_slices(self, using_copy_on_write):
81+
def test_setitem_cache_updating_slices(
82+
self, using_copy_on_write, warn_copy_on_write
83+
):
7884
# GH 7084
7985
# not updating cache on series setting with slices
8086
expected = DataFrame(
@@ -102,7 +108,8 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
102108
with tm.raises_chained_assignment_error():
103109
out[row["C"]][six:eix] = v
104110
else:
105-
out[row["C"]][six:eix] = v
111+
with tm.assert_cow_warning(warn_copy_on_write):
112+
out[row["C"]][six:eix] = v
106113

107114
if not using_copy_on_write:
108115
tm.assert_frame_equal(out, expected)
@@ -113,17 +120,21 @@ def test_setitem_cache_updating_slices(self, using_copy_on_write):
113120

114121
out = DataFrame({"A": [0, 0, 0]}, index=date_range("5/7/2014", "5/9/2014"))
115122
for ix, row in df.iterrows():
116-
out.loc[six:eix, row["C"]] += row["D"]
123+
# TODO(CoW-warn) should not warn
124+
with tm.assert_produces_warning(FutureWarning):
125+
out.loc[six:eix, row["C"]] += row["D"]
117126

118127
tm.assert_frame_equal(out, expected)
119128
tm.assert_series_equal(out["A"], expected["A"])
120129

121-
def test_altering_series_clears_parent_cache(self, using_copy_on_write):
130+
def test_altering_series_clears_parent_cache(
131+
self, using_copy_on_write, warn_copy_on_write
132+
):
122133
# GH #33675
123134
df = DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["A", "B"])
124135
ser = df["A"]
125136

126-
if using_copy_on_write:
137+
if using_copy_on_write or warn_copy_on_write:
127138
assert "A" not in df._item_cache
128139
else:
129140
assert "A" in df._item_cache
@@ -138,7 +149,7 @@ def test_altering_series_clears_parent_cache(self, using_copy_on_write):
138149

139150

140151
class TestChaining:
141-
def test_setitem_chained_setfault(self, using_copy_on_write):
152+
def test_setitem_chained_setfault(self, using_copy_on_write, warn_copy_on_write):
142153
# GH6026
143154
data = ["right", "left", "left", "left", "right", "left", "timeout"]
144155
mdata = ["right", "left", "left", "left", "right", "left", "none"]
@@ -150,6 +161,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
150161
df.response[mask] = "none"
151162
tm.assert_frame_equal(df, DataFrame({"response": data}))
152163
else:
164+
# TODO(CoW-warn) should warn
165+
# with tm.assert_cow_warning(warn_copy_on_write):
153166
df.response[mask] = "none"
154167
tm.assert_frame_equal(df, DataFrame({"response": mdata}))
155168

@@ -161,6 +174,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
161174
df.response[mask] = "none"
162175
tm.assert_frame_equal(df, DataFrame({"response": data}))
163176
else:
177+
# TODO(CoW-warn) should warn
178+
# with tm.assert_cow_warning(warn_copy_on_write):
164179
df.response[mask] = "none"
165180
tm.assert_frame_equal(df, DataFrame({"response": mdata}))
166181

@@ -172,6 +187,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
172187
df.response[mask] = "none"
173188
tm.assert_frame_equal(df, df_original)
174189
else:
190+
# TODO(CoW-warn) should warn
191+
# with tm.assert_cow_warning(warn_copy_on_write):
175192
df.response[mask] = "none"
176193
tm.assert_frame_equal(df, DataFrame({"response": mdata, "response1": data}))
177194

@@ -183,7 +200,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
183200
df["A"].iloc[0] = np.nan
184201
expected = DataFrame({"A": ["foo", "bar", "bah", "foo", "bar"]})
185202
else:
186-
df["A"].iloc[0] = np.nan
203+
with tm.assert_cow_warning(warn_copy_on_write):
204+
df["A"].iloc[0] = np.nan
187205
expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]})
188206
result = df.head()
189207
tm.assert_frame_equal(result, expected)
@@ -193,7 +211,8 @@ def test_setitem_chained_setfault(self, using_copy_on_write):
193211
with tm.raises_chained_assignment_error():
194212
df.A.iloc[0] = np.nan
195213
else:
196-
df.A.iloc[0] = np.nan
214+
with tm.assert_cow_warning(warn_copy_on_write):
215+
df.A.iloc[0] = np.nan
197216
result = df.head()
198217
tm.assert_frame_equal(result, expected)
199218

@@ -636,7 +655,9 @@ def test_cache_updating2(self, using_copy_on_write):
636655
expected = Series([0, 0, 0, 2, 0], name="f")
637656
tm.assert_series_equal(df.f, expected)
638657

639-
def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
658+
def test_iloc_setitem_chained_assignment(
659+
self, using_copy_on_write, warn_copy_on_write
660+
):
640661
# GH#3970
641662
with option_context("chained_assignment", None):
642663
df = DataFrame({"aa": range(5), "bb": [2.2] * 5})
@@ -648,7 +669,8 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
648669
with tm.raises_chained_assignment_error():
649670
df["bb"].iloc[0] = 0.13
650671
else:
651-
df["bb"].iloc[0] = 0.13
672+
with tm.assert_cow_warning(warn_copy_on_write):
673+
df["bb"].iloc[0] = 0.13
652674

653675
# GH#3970 this lookup used to break the chained setting to 0.15
654676
df.iloc[ck]
@@ -657,7 +679,8 @@ def test_iloc_setitem_chained_assignment(self, using_copy_on_write):
657679
with tm.raises_chained_assignment_error():
658680
df["bb"].iloc[0] = 0.15
659681
else:
660-
df["bb"].iloc[0] = 0.15
682+
with tm.assert_cow_warning(warn_copy_on_write):
683+
df["bb"].iloc[0] = 0.15
661684

662685
if not using_copy_on_write:
663686
assert df["bb"].iloc[0] == 0.15

pandas/tests/indexing/test_iat.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Series,
66
period_range,
77
)
8+
import pandas._testing as tm
89

910

1011
def test_iat(float_frame):
@@ -30,17 +31,22 @@ def test_iat_getitem_series_with_period_index():
3031
assert expected == result
3132

3233

33-
def test_iat_setitem_item_cache_cleared(indexer_ial, using_copy_on_write):
34+
def test_iat_setitem_item_cache_cleared(
35+
indexer_ial, using_copy_on_write, warn_copy_on_write
36+
):
3437
# GH#45684
3538
data = {"x": np.arange(8, dtype=np.int64), "y": np.int64(0)}
3639
df = DataFrame(data).copy()
3740
ser = df["y"]
3841

3942
# previously this iat setting would split the block and fail to clear
4043
# the item_cache.
41-
indexer_ial(df)[7, 0] = 9999
44+
with tm.assert_cow_warning(warn_copy_on_write and indexer_ial is tm.iloc):
45+
indexer_ial(df)[7, 0] = 9999
4246

43-
indexer_ial(df)[7, 1] = 1234
47+
# TODO(CoW-warn) should also warn for iat?
48+
with tm.assert_cow_warning(warn_copy_on_write and indexer_ial is tm.iloc):
49+
indexer_ial(df)[7, 1] = 1234
4450

4551
assert df.iat[7, 1] == 1234
4652
if not using_copy_on_write:

0 commit comments

Comments
 (0)