Description
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
Calling df.agg([function])
is much slower than df.agg(function)
when there are many columns and few rows.
I apologize if this is a known issue, I could not find a reference based on keywords that come to mind. Inspired by this SO question.
from timeit import timeit
import pandas as pd # version 1.4.0
import numpy as np # version 1.22.1
np.random.seed(0)
num_cols = 1000
df_ten_rows = pd.DataFrame(np.random.randint(0, 10, size=(10, num_cols)))
df_10k_rows = pd.DataFrame(np.random.randint(0, 10, size=(10_000, num_cols)))
assert pd.DataFrame(df_ten_rows.agg("sum").rename("sum")).T.equals(df_ten_rows.agg(["sum"]))
assert pd.DataFrame(df_10k_rows.agg("sum").rename("sum")).T.equals(df_10k_rows.agg(["sum"]))
setup = """
import pandas as pd # version 1.4.0
import numpy as np # version 1.22.1
np.random.seed(0)
num_cols = 1000
df_ten_rows = pd.DataFrame(np.random.randint(0, 10, size=(10, num_cols)))
df_10k_rows = pd.DataFrame(np.random.randint(0, 10, size=(10_000, num_cols)))
"""
number = 10 # nubmer of repetitions
codes = {
"10 rows, agg on sum": 'pd.DataFrame(df_ten_rows.agg("sum").rename("sum")).T',
"10 rows, agg on [sum]": 'df_ten_rows.agg(["sum"])',
"10k rows, agg on sum": 'pd.DataFrame(df_10k_rows.agg("sum").rename("sum")).T',
"10k rows, agg on [sum]": 'df_10k_rows.agg(["sum"])',
}
times = {
description: timeit(code, setup=setup, number=number)/number
for description, code in codes.items()
}
# {
# '10 rows, agg on sum': 0.005952695199812297,
# '10 rows, agg on [sum]': 2.789351126600013,
# '10k rows, agg on sum': 0.018355781599893817,
# '10k rows, agg on [sum]': 2.341517233000195
# }
Installed Versions
INSTALLED VERSIONS
commit : bb1f651
python : 3.8.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-1028-gcp
Version : #32~20.04.1-Ubuntu SMP Wed Jan 12 20:08:27 UTC 2022
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.0
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.dev0
setuptools : 56.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.4.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.4.23
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
Prior Performance
No response