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: do not use 'count' function name for value_counts() series/column name #53166

Closed
2 of 3 tasks
DnaAnalyticsOld opened this issue May 10, 2023 · 2 comments
Closed
2 of 3 tasks
Labels
API - Consistency Internal Consistency of API/Behavior Bug Closing Candidate May be closeable, needs more eyeballs

Comments

@DnaAnalyticsOld
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

1. value_counts = df.ColumnName.value_counts()
2. # make desired dataframe
3. df = pd.DataFrame(value_counts)
4. df.reset_index(drop=False, inplace=True)
5. df.rename(columns={'count': 'Hits'}, inplace=True)
6. new_df = df[df.Hits > max_hits]

Issue Description

On line 1 and/or line 3 value_counts/pandas assigns the name 'count' to the series/column.

'count' is a function name in pandas.

Line 6 reveals the issue with assigning the series/column name 'count'.

df = df[df.count > n]

-> Python/pandas error "cannot compare function and int".

Workaround (line 5) eliminates the error. I suppose df[df['count'] > n] is also a workaround.

My suggestion is to use a name other than 'count' (or any other pandas function name) when value_count/pandas creates the series/dataframe. Maybe 'Count'?

Expected Behavior

value_counts series/column name does not conflict with dataframe/series function name.

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.90.1-microsoft-standard-WSL2
Version : #1 SMP Fri Jan 27 02:56:13 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 66.0.0
pip : 23.1.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 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
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
zstandard : 0.21.0
tzdata : 2023.3
qtpy : None
pyqt5 : None
None

@DnaAnalyticsOld DnaAnalyticsOld added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 10, 2023
@topper-123
Copy link
Contributor

Any other name will likely also clash with something for someone. Do you have suggestion?

Speaking more generally than this there will always be the risk of clashes when using . to access a column. To avoid that us square brackets or loc.

@topper-123 topper-123 added API - Consistency Internal Consistency of API/Behavior Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 12, 2023
@phofl
Copy link
Member

phofl commented Dec 7, 2023

We changed this recently, so changing again is something we would want to avoid. Closing for now

@phofl phofl closed this as completed Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

3 participants