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 2 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
12 changes: 11 additions & 1 deletion pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import tokenize
import warnings

import numpy as np

from pandas._libs.lib import no_default
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_bool_kwarg
Expand Down Expand Up @@ -384,7 +386,15 @@ def eval(
try:
with warnings.catch_warnings(record=True):
# TODO: Filter the warnings we actually care about here.
target[assigner] = ret
if (
inplace is True
and hasattr(target, "columns")
and assigner in target.columns
):
loc = target.columns.get_loc(assigner)
target._iset_item_mgr(loc, np.array(ret), inplace=inplace)
Copy link
Member

Choose a reason for hiding this comment

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

We should not use this here. Please use loc for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, that helps a lot.

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