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 7 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
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
-
- Fixed regression in :meth:`DataFrame.eval` called with ``inplace=True`` had no effect (:issue:`47449`)

.. ---------------------------------------------------------------------------

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ def eval(
try:
with warnings.catch_warnings(record=True):
# TODO: Filter the warnings we actually care about here.
target[assigner] = ret
if hasattr(target, "loc"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal. What could target be here? Dict, series, DataFrame, something else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, basically these 3. Used ´isinstance(target, (DataFrame, Series))` instead

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
8 changes: 8 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,14 @@ def test_negate_lt_eq_le(engine, parser):
tm.assert_frame_equal(result, expected)


def test_set_inplace():
# GH 47449
df = DataFrame({"A": range(1, 6), "B": range(10, 0, -2), "C": range(11, 16)})
expected = Series([21, 20, 19, 18, 17], index=[0, 1, 2, 3, 4], name="A")
df.eval("A = B + C", inplace=True)
tm.assert_series_equal(df["A"], expected)


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