PERF: Series.value_counts() in aggregation function is slower than collections.Counter() #49728
Closed
2 of 3 tasks
Labels
Needs Info
Clarification about behavior needed to assess issue
Performance
Memory or execution speed performance
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
Series.value_counts() are slower especially if it counts for a small size of iterable elements.
Case1: value_counts() for counting small iterable elements
The picture below is the result of simulation 1 where I compared the speed of counting values for Series.value_counts() and collections.Counter(). In this simulation, I measured time for counting iterables of variable sizes ($N \in 10^1 - 10^7$ ) using Series.value_counts() and collections.Counter(). It shows Series.value_counts() comes to be slower if the $N < 10^3$ .
Source code for simulation 1:
Case2: repeatedly calls value_counts() in aggregation functions
That also comes to be issue when using in aggregation functions. I run another simulation to compare the execution time for Series.value_counts() and collections.Counter() 2. This time I used counting methods in an aggregate function. It also shows Series.value_counts() is 5 times slower than collections.Counter() method.
source code for simulation 2:
Is this an expected performance for value_counts()? If it isn't performance improvement should be cared.
Installed Versions
pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : 0.8.3
fsspec : 2022.10.0
gcsfs : None
matplotlib : 3.6.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
Prior Performance
No response
Footnotes
https://gist.github.com/bilzard/558f07a7d13f62b4222a92446704cbe5 ↩
https://gist.github.com/bilzard/08eb013b7a65e35e7bc8f0768c4bd147 ↩
The text was updated successfully, but these errors were encountered: