Skip to content

REGR: fix eval with inplace=True to correctly update column values inplace #47550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
90e2955
fix pre-commit issues
CloseChoice Jun 29, 2022
39987cf
fix linting errors
CloseChoice Jun 29, 2022
517c928
add inplace argument to isetitem and use in eval
CloseChoice Jun 29, 2022
c0d8502
changes due to PR discussions
CloseChoice Jun 29, 2022
7d2c398
fix issues
CloseChoice Jun 29, 2022
ba32bd5
update eval
CloseChoice Jun 29, 2022
cead80f
update whatsnew
CloseChoice Jun 30, 2022
e52895e
add PR suggestions
CloseChoice Jul 11, 2022
bdf5e00
Merge branch 'main' of github.com:pandas-dev/pandas into 2022-06-29-R…
CloseChoice Jul 11, 2022
e3e34cc
update imports in eval.py
CloseChoice Jul 11, 2022
541263c
Merge branch 'main' of github.com:pandas-dev/pandas into 2022-06-29-R…
CloseChoice Jul 16, 2022
8fdc63d
Merge branch 'main' of github.com:pandas-dev/pandas into 2022-06-29-R…
CloseChoice Aug 9, 2022
283c20a
check inplace and use NDFrame + small update to test
jorisvandenbossche Aug 12, 2022
9615c0e
Merge remote-tracking branch 'upstream/main' into 2022-06-29-REGR-47449
jorisvandenbossche Aug 12, 2022
e74b049
update test to use using_copy_on_write
jorisvandenbossche Aug 12, 2022
8ed21d5
Merge branch 'main' into 2022-06-29-REGR-47449
CloseChoice Aug 12, 2022
2ffb81c
Merge branch 'main' of github.com:pandas-dev/pandas into 2022-06-29-R…
CloseChoice Aug 17, 2022
76327eb
Merge branch '2022-06-29-REGR-47449' of github.com:CloseChoice/pandas…
CloseChoice Aug 17, 2022
32afe96
Merge remote-tracking branch 'upstream/main' into 2022-06-29-REGR-47449
jorisvandenbossche Aug 19, 2022
9959800
skip test for array manager
jorisvandenbossche Aug 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
- Fixed regression in :meth:`DataFrame.eval` creating a copy when updating inplace (:issue:`47449`)
-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from pandas.core.computation.parsing import tokenize_string
from pandas.core.computation.scope import ensure_scope
from pandas.core.generic import NDFrame

from pandas.io.formats.printing import pprint_thing

Expand Down Expand Up @@ -387,7 +388,10 @@ def eval(
try:
with warnings.catch_warnings(record=True):
# TODO: Filter the warnings we actually care about here.
target[assigner] = ret
if inplace and isinstance(target, NDFrame):
target.loc[:, assigner] = ret
else:
target[assigner] = ret
except (TypeError, IndexError) as err:
raise ValueError("Cannot assign expression output to target") from err

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,26 @@ def test_eval_no_support_column_name(request, column):
tm.assert_frame_equal(result, expected)


@td.skip_array_manager_not_yet_implemented
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"]
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:
tm.assert_series_equal(ser, expected["A"])
tm.assert_series_equal(result_view["A"], expected["A"])
else:
expected = Series([1, 2, 3], name="A")
tm.assert_series_equal(ser, expected)
tm.assert_series_equal(result_view["A"], expected)


class TestValidate:
@pytest.mark.parametrize("value", [1, "True", [1, 2, 3], 5.0])
def test_validate_bool_args(self, value):
Expand Down