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: .value_counts converts pd.NA to np.nan #42851

Open
2 of 3 tasks
JarnoRFB opened this issue Aug 2, 2021 · 2 comments
Open
2 of 3 tasks

BUG: .value_counts converts pd.NA to np.nan #42851

JarnoRFB opened this issue Aug 2, 2021 · 2 comments
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@JarnoRFB
Copy link
Contributor

JarnoRFB commented Aug 2, 2021

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

pd.Series(["a", "a", "b", pd.NA]).value_counts(dropna=False).index
Index(['a', 'b', nan], dtype='object')

Problem description

.value_counts converts pd.NA to np.nan. Instead pd.NA should be preserved. In my use case, I use value_counts to find small groups, based on which I want to filter the data. This is hard, if the value in the counts differs from the actual value.

Expected Output

Index(['a', 'b', <NA>], dtype='object')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.7.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-80-generic
Version : #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.1
numpy : 1.20.2
pytz : 2020.5
dateutil : 2.8.1
pip : 19.3.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : 6.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : None

@JarnoRFB JarnoRFB added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 2, 2021
@MarcoGorelli MarcoGorelli changed the title BUG: BUG: .value_counts converts pd.NA to np.nan Aug 10, 2021
@MarcoGorelli MarcoGorelli added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Aug 10, 2021
@MarcoGorelli
Copy link
Member

Thanks for the report, can confirm this reproduces on master

@MarcoGorelli MarcoGorelli removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 10, 2021
@phofl
Copy link
Member

phofl commented Aug 14, 2021

related to #33066 and #32931

This would vanish with an extension dtype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

3 participants