Skip to content

BUG: Cartesian product in outer merge with duplicated rows #46795

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

Closed
2 of 3 tasks
antonchasnyk opened this issue Apr 17, 2022 · 5 comments
Closed
2 of 3 tasks

BUG: Cartesian product in outer merge with duplicated rows #46795

antonchasnyk opened this issue Apr 17, 2022 · 5 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Needs Info Clarification about behavior needed to assess issue

Comments

@antonchasnyk
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
a = pd.DataFrame({'timestamp': ['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-03'],
                      'A': [1, 2, 3, 3]}).set_index('timestamp')
b = pd.DataFrame({'timestamp': ['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-03'],
                      'B': [1, 2, 3, 3]}).set_index('timestamp')
df = reduce(lambda l, r: pd.merge(l, r, left_index=True, right_index=True, how="outer"), [a, b])
print(df)

Issue Description

If the left and right data frame has duplicated rows then these rows will produce cartesian multiplicated rows in the result.

            A  B
timestamp       
2000-01-01  1  1
2000-01-02  2  2
2000-01-03  3  3
2000-01-03  3  3
2000-01-03  3  3 <---
2000-01-03  3  3 <---

this could potentially produce a huge data frame.

Expected Behavior

            A  B
timestamp       
2000-01-01  1  1
2000-01-02  2  2
2000-01-03  3  3
2000-01-03  3  3

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-107-generic
Version : #121-Ubuntu SMP Thu Mar 24 16:04:27 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : ru_RU.UTF-8
LOCALE : ru_RU.UTF-8

pandas : 1.4.1
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 20.0.2
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.35
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@antonchasnyk antonchasnyk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 17, 2022
@antonchasnyk antonchasnyk changed the title BUG: BUG: Cartesian product in outer merge with duplicated rows Apr 17, 2022
@GYHHAHA
Copy link
Contributor

GYHHAHA commented Apr 17, 2022

What's the problem here? Since we have 2 rows for 2000-01-03 in a and 2 rows for 2000-01-03 in b, then we will indeed produce 2*2 rows for 2000-01-03 in the final result.

@attack68
Copy link
Contributor

What's the problem here? Since we have 2 rows for 2000-01-03 in a and 2 rows for 2000-01-03 in b, then we will indeed produce 2*2 rows for 2000-01-03 in the final result.

Agreed, the input dataframes are specifically coded with duplicate values on the index so the output will also have duplicates.

@attack68 attack68 added Needs Info Clarification about behavior needed to assess issue Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Apr 18, 2022
@mroeschke
Copy link
Member

Thanks for the question but this is the expected behavior, you can first drop duplicate index labels or groupby + aggregate in first in order for the outer merge result to have unique labels

@antonchasnyk
Copy link
Author

Maybe you can throw a warning in this case? Because merging multicolumn dataframes can consume a lot of RAM. In my case, a few duplicated rows consumed 8Gb RAM for dataframe with the expected result shape (40150, 715).

@attack68
Copy link
Contributor

A warning is not required, you are expected to check if the index contains duplicates, and drop if required, see @mroeschke. Note that not all use cases are like yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

4 participants