Skip to content
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

feat(python): ensure string on parameters to join/join_asof are unique #11856

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
27 changes: 23 additions & 4 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3603,11 +3603,10 @@ def join_asof(
raise ValueError("you should pass the column to join on as an argument")

if by is not None:
by_left_ = [by] if isinstance(by, str) else by
by_right_ = by_left_
by_left_ = by_right_ = [by] if isinstance(by, str) else list(set(by))
elif (by_left is not None) and (by_right is not None):
by_left_ = [by_left] if isinstance(by_left, str) else by_left
by_right_ = [by_right] if isinstance(by_right, str) else by_right
by_left_ = [by_left] if isinstance(by_left, str) else list(set(by_left))
by_right_ = [by_right] if isinstance(by_right, str) else list(set(by_right))
else:
# no by
by_left_ = None
Expand Down Expand Up @@ -3792,11 +3791,31 @@ def join(
)
)

def _process_args(
input: str | Expr | Sequence[str | Expr],
) -> Expr | Sequence[str | Expr]:
# set strings to list and remove duplicates from list
if isinstance(input, str):
input = [input]
elif isinstance(input, Sequence):
# isolate string args and remove duplicates, preserving original order
input = list(enumerate(input)) # type: ignore[arg-type]
str_args = [x for x in input if isinstance(x[1], str)] # type: ignore[index]
nonstr_args = [x for x in input if not isinstance(x[1], str)] # type: ignore[index]
str_args = list(set(str_args)) # remove duplicates
input = [*str_args, *nonstr_args]
input.sort()
input = [x[1] for x in input] # type: ignore[index]
return input

if on is not None:
on = _process_args(on)
pyexprs = parse_as_list_of_expressions(on)
pyexprs_left = pyexprs
pyexprs_right = pyexprs
elif left_on is not None and right_on is not None:
left_on = _process_args(left_on)
right_on = _process_args(right_on)
pyexprs_left = parse_as_list_of_expressions(left_on)
pyexprs_right = parse_as_list_of_expressions(right_on)
else:
Expand Down