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

BUG: combine_first major performance regression #55845

Closed
2 of 3 tasks
arnaudlegout opened this issue Nov 6, 2023 · 3 comments · Fixed by #57034
Closed
2 of 3 tasks

BUG: combine_first major performance regression #55845

arnaudlegout opened this issue Nov 6, 2023 · 3 comments · Fixed by #57034
Labels
Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version Series Series data structure
Milestone

Comments

@arnaudlegout
Copy link
Contributor

arnaudlegout commented Nov 6, 2023

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
import numpy as np
import time

n = 40_000_000
addr_id2 = pd.Series(np.random.randint(1, high=1000000, size=n, dtype=np.uint32), dtype='UInt32')
addr_id1 = addr_id2.copy()

# Randomly replace 1% of addr_id1 with NA values (no specific impact of the percentage)
na_indices = np.random.choice(n, size=int(0.01 * n), replace=False)
addr_id1.iloc[na_indices] = pd.NA

df = pd.DataFrame({
    'addr_id1': addr_id1,
    'addr_id2': addr_id2
})

start = time.time()
df["addr_id1"].combine_first(df["addr_id2"])
print(time.time()-start)

Issue Description

In the reproducible example, the combine_first method takes 0.33s to run on my laptop with pandas 2.0.3, and 17s to runs on pandas 2.1.2

Expected Behavior

I expect combine_first to run as fast in pandas 2.1.2 as in pandas 2.0.3.

In my production code with production dataset, the performance penalty is even higher.
In the reproducible example combine_first is 52 times slower. On my production dataset with 1B rows,
combine_first usually runs in 70s, but after 2 days of computation, I interrupted it without getting a result.

I can explore more if needed.

Installed Versions

In [17]: pd.show_versions()

INSTALLED VERSIONS

commit : a60ad39
python : 3.11.5.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.6-200.fc38.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Fri Oct 6 19:02:35 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.2
numpy : 1.24.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3
Cython : 3.0.0
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : 0.10.0
bs4 : 4.12.2
bottleneck : 1.3.5
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.9.2
gcsfs : None
matplotlib : 3.8.0
numba : 0.57.1
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.3
sqlalchemy : 2.0.21
tables : None
tabulate : 0.8.10
xarray : 2023.6.0
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.2.0
pyqt5 : None

@arnaudlegout arnaudlegout added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 6, 2023
@arnaudlegout
Copy link
Contributor Author

The performance regression is still there in pandas 2.1.4

@lukemanley
Copy link
Member

Thanks for the report. #57034 should address this.

Side note: since the two series in the original example above share the same index and are already aligned you could use df["addr_id1"].fillna(df["addr_id2"]) in that case.

@lukemanley lukemanley added Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version Series Series data structure and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 23, 2024
@lukemanley lukemanley added this to the 2.2.1 milestone Jan 23, 2024
@arnaudlegout
Copy link
Contributor Author

thanks for the fix and for suggesting the usage of fillna, my usecase is indeed always on aligned indexes,and fillna is faster than combine_first on this usecase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version Series Series data structure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants