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

Issue merging on multiple columns #2304

Closed
Pankick opened this issue Sep 20, 2021 · 4 comments
Closed

Issue merging on multiple columns #2304

Pankick opened this issue Sep 20, 2021 · 4 comments

Comments

@Pankick
Copy link

Pankick commented Sep 20, 2021

Issue

When merging two data frames on multiple columns, the result is wrong.
This issue was caught on merging two large parquet data, but is reproducible with small samples below:

import pandas as pd
import numpy as np
import duckdb
duckdb.__version__

'0.2.9'

df1 = pd.DataFrame({
    'id_1': [1, 1, 1, 2, 2],
    'agedate': np.array(['2010-01-01','2010-02-01','2010-03-01','2020-02-01', '2020-03-01']).astype('datetime64[D]'),
    'age': [1, 2, 3, 1, 2],
    'v': [1.1, 1.2, 1.3, 2.1, 2.2]
})

df2 = pd.DataFrame({
    'id_1': [1, 1, 2],
    'agedate': np.array(['2010-01-01','2010-02-01', '2020-03-01']).astype('datetime64[D]'),
    'v2': [11.1, 11.2, 21.2]
})

Results when merging with Pandas:

pd_result = df1.merge(df2, on=['id_1', 'agedate'], how='left')
pd_result
  id_1 agedate age v v2
0 1 2010-01-01 1 1.1 11.1
1 1 2010-02-01 2 1.2 11.2
2 1 2010-03-01 3 1.3 NaN
3 2 2020-02-01 1 2.1 NaN
4 2 2020-03-01 2 2.2 21.2

Results when merging with duckdb:

con = duckdb.connect()
con.register('df1', df1)
con.register('df2', df2)
query = """SELECT * from df1
LEFT  OUTER JOIN df2
ON (df1.id_1=df2.id_1 and df1.agedate=df2.agedate)"""

result = con.execute(query).fetchdf()
result
  id_1 agedate age v v2
0 1.0 2010-01-01 1 1.1 11.1
1 1.0 2010-02-01 2 1.2 11.2
2 2.0 2020-03-01 2 2.2 21.2
3 NaN NaT 3 1.3 NaN
4 NaN NaT 1 2.1 NaN
This happens either merging with parquet data or in-memory data.
@hannes
Copy link
Member

hannes commented Sep 22, 2021

Maybe @pdet can have a look when he's awake again?

@pdet
Copy link
Member

pdet commented Sep 22, 2021

The issue here is that the data frames that are being joined have columns with the same name, so the id_1 agedate we see in the result from duckdb are actually from df2.
I'll make a change to add a sufix counter of fetchdf() outputs if there are multiple columns with the same name.

@Pankick Meanwhile, a workaround for this issue is projecting the columns you want. e.g.,

SELECT df1.id_1, df1.agedate ,age,v,v2 from df1
LEFT  OUTER JOIN df2
ON (df1.id_1=df2.id_1 and df1.agedate=df2.agedate) 

pdet added a commit to pdet/duckdb that referenced this issue Sep 22, 2021
…om the query result have the same name and are being fetched to a pandas df
Mytherin added a commit that referenced this issue Sep 30, 2021
@omo
Copy link
Contributor

omo commented May 6, 2023

Unsolicited triage bot here: It seems this can be closed? @pdet

@pdet
Copy link
Member

pdet commented May 6, 2023

Looks like it, thanks for pointing it out! :-)

@pdet pdet closed this as completed May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants