Skip to content

Commit

Permalink
Remove Copy-on-Write warning mode from tests (pandas-dev#57237)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and pmhatre1 committed May 7, 2024
1 parent 0c06dd3 commit 5c62ace
Show file tree
Hide file tree
Showing 44 changed files with 317 additions and 788 deletions.
8 changes: 0 additions & 8 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1966,14 +1966,6 @@ def using_copy_on_write() -> bool:
return True


@pytest.fixture
def warn_copy_on_write() -> bool:
"""
Fixture to check if Copy-on-Write is in warning mode.
"""
return False


@pytest.fixture
def using_infer_string() -> bool:
"""
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ def test_apply_dtype(col):
tm.assert_series_equal(result, expected)


def test_apply_mutating(using_copy_on_write, warn_copy_on_write):
def test_apply_mutating(using_copy_on_write):
# GH#35462 case where applied func pins a new BlockManager to a row
df = DataFrame({"a": range(100), "b": range(100, 200)})
df_orig = df.copy()
Expand All @@ -1501,8 +1501,7 @@ def func(row):
expected = df.copy()
expected["a"] += 1

with tm.assert_cow_warning(warn_copy_on_write):
result = df.apply(func, axis=1)
result = df.apply(func, axis=1)

tm.assert_frame_equal(result, expected)
if using_copy_on_write:
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ def test_assignment_not_inplace(self):
expected["c"] = expected["a"] + expected["b"]
tm.assert_frame_equal(df, expected)

def test_multi_line_expression(self, warn_copy_on_write):
def test_multi_line_expression(self):
# GH 11149
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
expected = df.copy()
Expand Down Expand Up @@ -1964,15 +1964,14 @@ def test_eval_no_support_column_name(request, column):
tm.assert_frame_equal(result, expected)


def test_set_inplace(using_copy_on_write, warn_copy_on_write):
def test_set_inplace(using_copy_on_write):
# https://github.com/pandas-dev/pandas/issues/47449
# Ensure we don't only update the DataFrame inplace, but also the actual
# column values, such that references to this column also get updated
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
result_view = df[:]
ser = df["A"]
with tm.assert_cow_warning(warn_copy_on_write):
df.eval("A = B + C", inplace=True)
df.eval("A = B + C", inplace=True)
expected = DataFrame({"A": [11, 13, 15], "B": [4, 5, 6], "C": [7, 8, 9]})
tm.assert_frame_equal(df, expected)
if not using_copy_on_write:
Expand Down
30 changes: 12 additions & 18 deletions pandas/tests/copy_view/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ def index_view(index_data):
return idx, view


def test_set_index_update_column(using_copy_on_write, warn_copy_on_write):
def test_set_index_update_column(using_copy_on_write):
df = DataFrame({"a": [1, 2], "b": 1})
df = df.set_index("a", drop=False)
expected = df.index.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
df.iloc[0, 0] = 100
df.iloc[0, 0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
Expand All @@ -40,53 +39,49 @@ def test_set_index_drop_update_column(using_copy_on_write):
tm.assert_index_equal(df.index, expected)


def test_set_index_series(using_copy_on_write, warn_copy_on_write):
def test_set_index_series(using_copy_on_write):
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
df = df.set_index(ser)
expected = df.index.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
ser.iloc[0] = 100
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))


def test_assign_index_as_series(using_copy_on_write, warn_copy_on_write):
def test_assign_index_as_series(using_copy_on_write):
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
df.index = ser
expected = df.index.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
ser.iloc[0] = 100
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))


def test_assign_index_as_index(using_copy_on_write, warn_copy_on_write):
def test_assign_index_as_index(using_copy_on_write):
df = DataFrame({"a": [1, 2], "b": 1.5})
ser = Series([10, 11])
rhs_index = Index(ser)
df.index = rhs_index
rhs_index = None # overwrite to clear reference
expected = df.index.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
ser.iloc[0] = 100
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(df.index, expected)
else:
tm.assert_index_equal(df.index, Index([100, 11]))


def test_index_from_series(using_copy_on_write, warn_copy_on_write):
def test_index_from_series(using_copy_on_write):
ser = Series([1, 2])
idx = Index(ser)
expected = idx.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
ser.iloc[0] = 100
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
else:
Expand All @@ -101,13 +96,12 @@ def test_index_from_series_copy(using_copy_on_write):
assert np.shares_memory(get_array(ser), arr)


def test_index_from_index(using_copy_on_write, warn_copy_on_write):
def test_index_from_index(using_copy_on_write):
ser = Series([1, 2])
idx = Index(ser)
idx = Index(idx)
expected = idx.copy(deep=True)
with tm.assert_cow_warning(warn_copy_on_write):
ser.iloc[0] = 100
ser.iloc[0] = 100
if using_copy_on_write:
tm.assert_index_equal(idx, expected)
else:
Expand Down
109 changes: 1 addition & 108 deletions pandas/tests/copy_view/test_chained_assignment_deprecation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pytest

from pandas.compat import PY311
from pandas.errors import (
ChainedAssignmentError,
SettingWithCopyWarning,
Expand Down Expand Up @@ -33,117 +32,11 @@ def test_methods_iloc_warn(using_copy_on_write):
df.iloc[:, 0].bfill(inplace=True)


@pytest.mark.parametrize(
"func, args",
[
("replace", (4, 5)),
("fillna", (1,)),
("interpolate", ()),
("bfill", ()),
("ffill", ()),
],
)
def test_methods_iloc_getitem_item_cache(
func, args, using_copy_on_write, warn_copy_on_write
):
# ensure we don't incorrectly raise chained assignment warning because
# of the item cache / iloc not setting the item cache
df_orig = DataFrame({"a": [1, 2, 3], "b": 1})

df = df_orig.copy()
ser = df.iloc[:, 0]
getattr(ser, func)(*args, inplace=True)

# parent that holds item_cache is dead, so don't increase ref count
df = df_orig.copy()
ser = df.copy()["a"]
getattr(ser, func)(*args, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
ser = df.iloc[:, 0] # iloc creates a new object
getattr(ser, func)(*args, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
ser = df["a"]
getattr(ser, func)(*args, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
# TODO(CoW-warn) because of the usage of *args, this doesn't warn on Py3.11+
if using_copy_on_write:
with tm.raises_chained_assignment_error(not PY311):
getattr(df["a"], func)(*args, inplace=True)
else:
with tm.assert_cow_warning(not PY311, match="A value"):
getattr(df["a"], func)(*args, inplace=True)

df = df_orig.copy()
ser = df["a"] # populate the item_cache and keep ref
if using_copy_on_write:
with tm.raises_chained_assignment_error(not PY311):
getattr(df["a"], func)(*args, inplace=True)
else:
# ideally also warns on the default mode, but the ser' _cacher
# messes up the refcount + even in warning mode this doesn't trigger
# the warning of Py3.1+ (see above)
with tm.assert_cow_warning(warn_copy_on_write and not PY311, match="A value"):
getattr(df["a"], func)(*args, inplace=True)


def test_methods_iloc_getitem_item_cache_fillna(
using_copy_on_write, warn_copy_on_write
):
# ensure we don't incorrectly raise chained assignment warning because
# of the item cache / iloc not setting the item cache
df_orig = DataFrame({"a": [1, 2, 3], "b": 1})

df = df_orig.copy()
ser = df.iloc[:, 0]
ser.fillna(1, inplace=True)

# parent that holds item_cache is dead, so don't increase ref count
df = df_orig.copy()
ser = df.copy()["a"]
ser.fillna(1, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
ser = df.iloc[:, 0] # iloc creates a new object
ser.fillna(1, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
ser = df["a"]
ser.fillna(1, inplace=True)

df = df_orig.copy()
df["a"] # populate the item_cache
if using_copy_on_write:
with tm.raises_chained_assignment_error():
df["a"].fillna(1, inplace=True)
else:
with tm.assert_cow_warning(match="A value"):
df["a"].fillna(1, inplace=True)

df = df_orig.copy()
ser = df["a"] # populate the item_cache and keep ref
if using_copy_on_write:
with tm.raises_chained_assignment_error():
df["a"].fillna(1, inplace=True)
else:
# TODO(CoW-warn) ideally also warns on the default mode, but the ser' _cacher
# messes up the refcount
with tm.assert_cow_warning(warn_copy_on_write, match="A value"):
df["a"].fillna(1, inplace=True)


# TODO(CoW-warn) expand the cases
@pytest.mark.parametrize(
"indexer", [0, [0, 1], slice(0, 2), np.array([True, False, True])]
)
def test_series_setitem(indexer, using_copy_on_write, warn_copy_on_write):
def test_series_setitem(indexer, using_copy_on_write):
# ensure we only get a single warning for those typical cases of chained
# assignment
df = DataFrame({"a": [1, 2, 3], "b": 1})
Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/copy_view/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
from pandas.tests.copy_view.util import get_array


def test_clip_inplace_reference(using_copy_on_write, warn_copy_on_write):
def test_clip_inplace_reference(using_copy_on_write):
df = DataFrame({"a": [1.5, 2, 3]})
df_copy = df.copy()
arr_a = get_array(df, "a")
view = df[:]
if warn_copy_on_write:
with tm.assert_cow_warning():
df.clip(lower=2, inplace=True)
else:
df.clip(lower=2, inplace=True)
df.clip(lower=2, inplace=True)

if using_copy_on_write:
assert not np.shares_memory(get_array(df, "a"), arr_a)
Expand Down
Loading

0 comments on commit 5c62ace

Please sign in to comment.