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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 8 additions & 3 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

0 commit comments

Comments
 (0)