Skip to content

Commit

Permalink
Fix approach to detecting assignment for gte/lte operators (#13285)
Browse files Browse the repository at this point in the history
Resolves #13185

Authors:
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #13285
  • Loading branch information
vyasr authored May 11, 2023
1 parent 8a5a1f4 commit deaba97
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7139,13 +7139,14 @@ def eval(self, expr: str, inplace: bool = False, **kwargs):
"Keyword arguments other than `inplace` are not supported"
)

# Have to use a regex match to avoid capturing "=="
includes_assignment = re.search("[^=]=[^=]", expr) is not None
# Have to use a regex match to avoid capturing ==, >=, or <=
equals_sign_regex = "[^=><]=[^=]"
includes_assignment = re.search(equals_sign_regex, expr) is not None

# Check if there were multiple statements. Filter out empty lines.
statements = tuple(filter(None, expr.strip().split("\n")))
if len(statements) > 1 and any(
re.search("[^=]=[^=]", st) is None for st in statements
re.search(equals_sign_regex, st) is None for st in statements
):
raise ValueError(
"Multi-line expressions are only valid if all expressions "
Expand Down
3 changes: 2 additions & 1 deletion python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9794,6 +9794,7 @@ def df_eval(request):
("a / b", float),
("a * b", int),
("a > b", int),
("a >= b", int),
("a > b > c", int),
("a > b < c", int),
("a & b", int),
Expand Down Expand Up @@ -9835,7 +9836,7 @@ def test_dataframe_eval(df_eval, expr, dtype):
assert_eq(expect, got, check_names=False)

# Test inplace
if re.search("[^=]=[^=]", expr) is not None:
if re.search("[^=><]=[^=]", expr) is not None:
pdf_eval = df_eval.to_pandas()
pdf_eval.eval(expr, inplace=True)
df_eval.eval(expr, inplace=True)
Expand Down

0 comments on commit deaba97

Please sign in to comment.