Skip to content

Commit 9d8e298

Browse files
sfc-gh-mvashishthasfc-gh-jkew
authored andcommitted
FIX-#7669: Respect eval(inplace=False). (#7670)
Prior to this commit, calling `DataFrame.eval(inplace=False, engine='python')` with an assignment would incorrectly update the input dataframe. The root cause was calling `copy(deep=None)` instead of `copy(deep=True)`. We need to insert new columns to a deep copy of the input dataframe. `deep=None` is not part of the `copy` API at all, so we don't need to fix `copy()` behavior. It happens that `deep=None` does a deep copy in pandas, which is where we got the `deep=None` code from. Resolves #7669 Signed-off-by: sfc-gh-mvashishtha <[email protected]>
1 parent 40671e1 commit 9d8e298

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

modin/core/computation/eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def eval(
381381
try:
382382
target = env.target
383383
if isinstance(target, BasePandasDataset):
384-
target = target.copy(deep=None)
384+
target = target.copy(deep=True)
385385
else:
386386
target = target.copy()
387387
except AttributeError as err:

modin/tests/pandas/dataframe/test_udf.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ def test_eval_scalar():
385385
assert df.eval("1") == 1
386386

387387

388+
@pytest.mark.parametrize("engine", ("numexpr", "python"))
389+
def test_eval_not_inplace_does_not_change_input_dataframe(engine):
390+
snow_df, pandas_df = create_test_dfs({"a": [1, 2, 3]})
391+
original_pandas = pandas_df.copy()
392+
snow_result = snow_df.eval("b = a + 1", inplace=False, engine=engine)
393+
pandas_result = pandas_df.eval("b = a + 1", inplace=False, engine=engine)
394+
df_equals(snow_df, original_pandas)
395+
df_equals(pandas_df, original_pandas)
396+
df_equals(snow_result, pandas_result)
397+
398+
388399
TEST_VAR = 2
389400

390401

0 commit comments

Comments
 (0)