Skip to content

PERF: regression on mean(axis=1) compared to old pandas version #60469

Open
@Remy-ooo

Description

@Remy-ooo

Pandas version checks

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

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

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

Reproducible Example

While working on upgrading pandas version on an old project, I found these perf regressions. mean(axis=1) seems around 30% slower on a dataframe with nans, then if there's a copy(), it becomes almost twice slower. I have tried profiling with no luck.

import pandas as pd
import numpy as np

p = 0.1

df = pd.DataFrame(np.random.rand(2988, 1471))
df.columns = df.columns.astype(str)
df_nan = df.mask(np.random.choice([True, False], size=df.shape, p=[p, 1 - p]))
df_nan_copy = df_nan.copy()

pandas 2.2.2:

In [4]: %timeit df_nan.mean(axis=1)
9.71 ms ± 66.9 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [5]: %timeit df_nan_copy.mean(axis=1)
16 ms ± 174 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

pandas 0.22.0 (python 3.6):

In [2]: %timeit df_nan.mean(axis=1)
6.68 ms ± 46 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]: %timeit df_nan_copy.mean(axis=1)
7.98 ms ± 94.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Installed Versions

INSTALLED VERSIONS ------------------ commit : d9cdd2e python : 3.12.4.final.0 python-bits : 64 OS : Darwin OS-release : 24.1.0 Version : Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:11 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6020 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.5.1
pip : 24.3.1
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 7.3.7
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.2.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.25.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.3.1
gcsfs : None
matplotlib : 3.8.4
numba : 0.59.1
numexpr : 2.8.7
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.3.1
scipy : 1.13.1
sqlalchemy : 2.0.30
tables : 3.9.2
tabulate : 0.9.0
xarray : 2023.6.0
xlrd : None
zstandard : 0.22.0
tzdata : 2023.3
qtpy : 2.4.1
pyqt5 : None

Prior Performance

pandas 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions