Skip to content

Commit 34f39a9

Browse files
authored
CoW: Add warning for fillna with inplace (#56064)
1 parent 9138b5b commit 34f39a9

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

pandas/core/generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -7180,6 +7180,18 @@ def fillna(
71807180
ChainedAssignmentError,
71817181
stacklevel=2,
71827182
)
7183+
elif not PYPY and not using_copy_on_write():
7184+
ctr = sys.getrefcount(self)
7185+
ref_count = REF_COUNT
7186+
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
7187+
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7188+
ref_count += 1
7189+
if ctr <= ref_count:
7190+
warnings.warn(
7191+
_chained_assignment_warning_method_msg,
7192+
FutureWarning,
7193+
stacklevel=2,
7194+
)
71837195

71847196
value, method = validate_fillna_kwargs(value, method)
71857197
if method is not None:

pandas/tests/copy_view/test_interp_fillna.py

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Series,
1111
Timestamp,
1212
interval_range,
13+
option_context,
1314
)
1415
import pandas._testing as tm
1516
from pandas.tests.copy_view.util import get_array
@@ -364,6 +365,17 @@ def test_fillna_chained_assignment(using_copy_on_write):
364365
with tm.raises_chained_assignment_error():
365366
df[["a"]].fillna(100, inplace=True)
366367
tm.assert_frame_equal(df, df_orig)
368+
else:
369+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
370+
with option_context("mode.chained_assignment", None):
371+
df[["a"]].fillna(100, inplace=True)
372+
373+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
374+
with option_context("mode.chained_assignment", None):
375+
df[df.a > 5].fillna(100, inplace=True)
376+
377+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
378+
df["a"].fillna(100, inplace=True)
367379

368380

369381
@pytest.mark.parametrize("func", ["interpolate", "ffill", "bfill"])

pandas/tests/frame/methods/test_fillna.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def test_fillna_on_column_view(self, using_copy_on_write):
5858
df[0].fillna(-1, inplace=True)
5959
assert np.isnan(arr[:, 0]).all()
6060
else:
61-
df[0].fillna(-1, inplace=True)
61+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
62+
df[0].fillna(-1, inplace=True)
6263
assert (arr[:, 0] == -1).all()
6364

6465
# i.e. we didn't create a new 49-column block

pandas/tests/frame/test_block_internals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ def test_update_inplace_sets_valid_block_values(using_copy_on_write):
429429
with tm.raises_chained_assignment_error():
430430
df["a"].fillna(1, inplace=True)
431431
else:
432-
df["a"].fillna(1, inplace=True)
432+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
433+
df["a"].fillna(1, inplace=True)
433434

434435
# check we haven't put a Series into any block.values
435436
assert isinstance(df._mgr.blocks[0].values, Categorical)

pandas/tests/indexing/multiindex/test_chaining_and_caching.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ def test_detect_chained_assignment(using_copy_on_write, warn_copy_on_write):
3434
with tm.raises_chained_assignment_error():
3535
zed["eyes"]["right"].fillna(value=555, inplace=True)
3636
elif warn_copy_on_write:
37-
# TODO(CoW-warn) should warn
38-
zed["eyes"]["right"].fillna(value=555, inplace=True)
37+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
38+
zed["eyes"]["right"].fillna(value=555, inplace=True)
3939
else:
4040
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
4141
with pytest.raises(SettingWithCopyError, match=msg):
42-
zed["eyes"]["right"].fillna(value=555, inplace=True)
42+
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
43+
zed["eyes"]["right"].fillna(value=555, inplace=True)
4344

4445

4546
@td.skip_array_manager_invalid_test # with ArrayManager df.loc[0] is not a view

0 commit comments

Comments
 (0)