Skip to content

Commit e0f3a18

Browse files
CoW: Fix warnings for eval (#56170)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 6669aee commit e0f3a18

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

pandas/core/computation/eval.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,10 @@ def eval(
388388
# we will ignore numpy warnings here; e.g. if trying
389389
# to use a non-numeric indexer
390390
try:
391-
with warnings.catch_warnings(record=True):
392-
warnings.filterwarnings(
393-
"always", "Setting a value on a view", FutureWarning
394-
)
395-
# TODO: Filter the warnings we actually care about here.
396-
if inplace and isinstance(target, NDFrame):
397-
target.loc[:, assigner] = ret
398-
else:
399-
target[ # pyright: ignore[reportGeneralTypeIssues]
400-
assigner
401-
] = ret
391+
if inplace and isinstance(target, NDFrame):
392+
target.loc[:, assigner] = ret
393+
else:
394+
target[assigner] = ret # pyright: ignore[reportGeneralTypeIssues]
402395
except (TypeError, IndexError) as err:
403396
raise ValueError("Cannot assign expression output to target") from err
404397

pandas/core/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,17 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
650650
Used in :meth:`DataFrame.eval`.
651651
"""
652652
from pandas.core.computation.parsing import clean_column_name
653+
from pandas.core.series import Series
653654

654655
if isinstance(self, ABCSeries):
655656
return {clean_column_name(self.name): self}
656657

657658
return {
658-
clean_column_name(k): v for k, v in self.items() if not isinstance(k, int)
659+
clean_column_name(k): Series(
660+
v, copy=False, index=self.index, name=k
661+
).__finalize__(self)
662+
for k, v in zip(self.columns, self._iter_column_arrays())
663+
if not isinstance(k, int)
659664
}
660665

661666
@final

pandas/tests/computation/test_eval.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,7 @@ def test_assignment_single_assign_new(self):
11731173
df.eval("c = a + b", inplace=True)
11741174
tm.assert_frame_equal(df, expected)
11751175

1176-
# TODO(CoW-warn) this should not warn (DataFrame.eval creates refs to self)
1177-
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
1178-
def test_assignment_single_assign_local_overlap(self, warn_copy_on_write):
1176+
def test_assignment_single_assign_local_overlap(self):
11791177
df = DataFrame(
11801178
np.random.default_rng(2).standard_normal((5, 2)), columns=list("ab")
11811179
)
@@ -1229,8 +1227,6 @@ def test_column_in(self):
12291227
tm.assert_series_equal(result, expected, check_names=False)
12301228

12311229
@pytest.mark.xfail(reason="Unknown: Omitted test_ in name prior.")
1232-
# TODO(CoW-warn) this should not warn (DataFrame.eval creates refs to self)
1233-
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
12341230
def test_assignment_not_inplace(self):
12351231
# see gh-9297
12361232
df = DataFrame(
@@ -1244,7 +1240,7 @@ def test_assignment_not_inplace(self):
12441240
expected["c"] = expected["a"] + expected["b"]
12451241
tm.assert_frame_equal(df, expected)
12461242

1247-
def test_multi_line_expression(self):
1243+
def test_multi_line_expression(self, warn_copy_on_write):
12481244
# GH 11149
12491245
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
12501246
expected = df.copy()
@@ -1917,8 +1913,8 @@ def test_set_inplace(using_copy_on_write, warn_copy_on_write):
19171913
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
19181914
result_view = df[:]
19191915
ser = df["A"]
1920-
# with tm.assert_cow_warning(warn_copy_on_write):
1921-
df.eval("A = B + C", inplace=True)
1916+
with tm.assert_cow_warning(warn_copy_on_write):
1917+
df.eval("A = B + C", inplace=True)
19221918
expected = DataFrame({"A": [11, 13, 15], "B": [4, 5, 6], "C": [7, 8, 9]})
19231919
tm.assert_frame_equal(df, expected)
19241920
if not using_copy_on_write:

0 commit comments

Comments
 (0)