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

DataFrame.join_asof incorrect result for by set to tuples #10427

Closed
2 tasks done
hleumas opened this issue Aug 11, 2023 · 4 comments · Fixed by #10447
Closed
2 tasks done

DataFrame.join_asof incorrect result for by set to tuples #10427

hleumas opened this issue Aug 11, 2023 · 4 comments · Fixed by #10447
Assignees
Labels
bug Something isn't working python Related to Python Polars

Comments

@hleumas
Copy link

hleumas commented Aug 11, 2023

Checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl

df1 = pl.DataFrame({
    'n': [10, 20, 30, 40, 50, 60],
    'id1': [0, 0, 3, 3, 5, 5],
    'id2': [1, 2, 1, 2, 1, 2],
    'x': ['a', 'b', 'c', 'd', 'e', 'f'],
}).sort(by='n')

df2 = pl.DataFrame({
    'n': [25, 8, 5, 23, 15, 35],
    'id1': [0, 0, 3, 3, 5, 5],
    'id2': [1, 2, 1, 2, 1, 2],
    'y': ['A', 'B', 'C', 'D', 'E', 'F'],
}).sort(by='n')

print('Incorrect join_asof')
print(df1.join_asof(df2, on='n', by=('id1', 'id2')))

print('Correct join_asof')
print(df1.join_asof(df2, on='n', by=['id1', 'id2']))

Issue description

Argument by of join_asof seems to be incorrectly ignored when tuple is supplied. Instead of expected result, we get this:

>>> df1.join_asof(df2, on='n', by=('id1', 'id2'))

shape: (6, 7)
┌─────┬─────┬─────┬─────┬───────────┬───────────┬─────┐
│ n   ┆ id1 ┆ id2 ┆ x   ┆ id1_right ┆ id2_right ┆ y   │
│ --- ┆ --- ┆ --- ┆ --- ┆ ---       ┆ ---       ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ str ┆ i64       ┆ i64       ┆ str │
╞═════╪═════╪═════╪═════╪═══════════╪═══════════╪═════╡
│ 10  ┆ 0   ┆ 1   ┆ a   ┆ 0         ┆ 2         ┆ B   │
│ 20  ┆ 0   ┆ 2   ┆ b   ┆ 5         ┆ 1         ┆ E   │
│ 30  ┆ 3   ┆ 1   ┆ c   ┆ 0         ┆ 1         ┆ A   │
│ 40  ┆ 3   ┆ 2   ┆ d   ┆ 5         ┆ 2         ┆ F   │
│ 50  ┆ 5   ┆ 1   ┆ e   ┆ 5         ┆ 2         ┆ F   │
│ 60  ┆ 5   ┆ 2   ┆ f   ┆ 5         ┆ 2         ┆ F   │
└─────┴─────┴─────┴─────┴───────────┴───────────┴─────┘

Expected behavior

When supplied a list, we get the correct result:

>>> df1.join_asof(df2, on='n', by=['id1', 'id2'])

shape: (6, 5)
┌─────┬─────┬─────┬─────┬──────┐
│ n   ┆ id1 ┆ id2 ┆ x   ┆ y    │
│ --- ┆ --- ┆ --- ┆ --- ┆ ---  │
│ i64 ┆ i64 ┆ i64 ┆ str ┆ str  │
╞═════╪═════╪═════╪═════╪══════╡
│ 10  ┆ 0   ┆ 1   ┆ a   ┆ null │
│ 20  ┆ 0   ┆ 2   ┆ b   ┆ B    │
│ 30  ┆ 3   ┆ 1   ┆ c   ┆ C    │
│ 40  ┆ 3   ┆ 2   ┆ d   ┆ D    │
│ 50  ┆ 5   ┆ 1   ┆ e   ┆ E    │
│ 60  ┆ 5   ┆ 2   ┆ f   ┆ F    │
└─────┴─────┴─────┴─────┴──────┘

Installed versions

--------Version info---------
Polars:              0.18.13
Index type:          UInt32
Platform:            macOS-13.4.1-arm64-arm-64bit
Python:              3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)]

----Optional dependencies----
adbc_driver_sqlite:  <not installed>
cloudpickle:         <not installed>
connectorx:          <not installed>
deltalake:           <not installed>
fsspec:              <not installed>
matplotlib:          3.7.2
numpy:               1.24.4
pandas:              2.0.3
pyarrow:             12.0.1
pydantic:            <not installed>
sqlalchemy:          <not installed>
xlsx2csv:            <not installed>
xlsxwriter:          <not installed>
@hleumas hleumas added bug Something isn't working python Related to Python Polars labels Aug 11, 2023
@cmdlineluser
Copy link
Contributor

Looks like it happens here:

if isinstance(by, str):
by_left_ = [by]
by_right_ = [by]
elif isinstance(by, list):
by_left_ = by
by_right_ = by

There is an explicit check for str and list

@mcrumiller
Copy link
Contributor

@cmdlineluser good find, the typing suggests we should take a sequence, which a tuple definitely is.

@zundertj zundertj self-assigned this Aug 13, 2023
zundertj added a commit to zundertj/polars that referenced this issue Aug 13, 2023
Closes pola-rs#10427.

I noticed that for the right by argument, there was some argument parsing code suggesting it could take expressions, but it will fail. So it is just strings and sequence of strings.
@zundertj
Copy link
Collaborator

Thank you for reporting this, I have raised a fix in #10447.

@hleumas
Copy link
Author

hleumas commented Aug 14, 2023

Thanks a lot everybody! I must say wow, you're reacting super fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants