Skip to content

Commit

Permalink
CoW: Add warning for fillna with inplace (#56064)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Nov 21, 2023
1 parent 9138b5b commit 34f39a9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7180,6 +7180,18 @@ def fillna(
ChainedAssignmentError,
stacklevel=2,
)
elif not PYPY and not using_copy_on_write():
ctr = sys.getrefcount(self)
ref_count = REF_COUNT
if isinstance(self, ABCSeries) and hasattr(self, "_cacher"):
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
ref_count += 1
if ctr <= ref_count:
warnings.warn(
_chained_assignment_warning_method_msg,
FutureWarning,
stacklevel=2,
)

value, method = validate_fillna_kwargs(value, method)
if method is not None:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/copy_view/test_interp_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Series,
Timestamp,
interval_range,
option_context,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
Expand Down Expand Up @@ -364,6 +365,17 @@ def test_fillna_chained_assignment(using_copy_on_write):
with tm.raises_chained_assignment_error():
df[["a"]].fillna(100, inplace=True)
tm.assert_frame_equal(df, df_orig)
else:
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
df[["a"]].fillna(100, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
with option_context("mode.chained_assignment", None):
df[df.a > 5].fillna(100, inplace=True)

with tm.assert_produces_warning(FutureWarning, match="inplace method"):
df["a"].fillna(100, inplace=True)


@pytest.mark.parametrize("func", ["interpolate", "ffill", "bfill"])
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def test_fillna_on_column_view(self, using_copy_on_write):
df[0].fillna(-1, inplace=True)
assert np.isnan(arr[:, 0]).all()
else:
df[0].fillna(-1, inplace=True)
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
df[0].fillna(-1, inplace=True)
assert (arr[:, 0] == -1).all()

# i.e. we didn't create a new 49-column block
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ def test_update_inplace_sets_valid_block_values(using_copy_on_write):
with tm.raises_chained_assignment_error():
df["a"].fillna(1, inplace=True)
else:
df["a"].fillna(1, inplace=True)
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
df["a"].fillna(1, inplace=True)

# check we haven't put a Series into any block.values
assert isinstance(df._mgr.blocks[0].values, Categorical)
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/indexing/multiindex/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def test_detect_chained_assignment(using_copy_on_write, warn_copy_on_write):
with tm.raises_chained_assignment_error():
zed["eyes"]["right"].fillna(value=555, inplace=True)
elif warn_copy_on_write:
# TODO(CoW-warn) should warn
zed["eyes"]["right"].fillna(value=555, inplace=True)
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
zed["eyes"]["right"].fillna(value=555, inplace=True)
else:
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
with pytest.raises(SettingWithCopyError, match=msg):
zed["eyes"]["right"].fillna(value=555, inplace=True)
with tm.assert_produces_warning(FutureWarning, match="inplace method"):
zed["eyes"]["right"].fillna(value=555, inplace=True)


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

0 comments on commit 34f39a9

Please sign in to comment.