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: Inconsistent results of value_counts and mode for different nan-values #39007

Closed
3 tasks done
realead opened this issue Jan 6, 2021 · 1 comment · Fixed by #39353
Closed
3 tasks done

BUG: Inconsistent results of value_counts and mode for different nan-values #39007

realead opened this issue Jan 6, 2021 · 1 comment · Fixed by #39353
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@realead
Copy link
Contributor

realead commented Jan 6, 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.

import pandas as pd
import numpy as np
s=pd.Series([True, pd.NA, np.nan])
s.value_counts(dropna=False)
print(s.value_counts(dropna=False))
print(s.mode(dropna=False))

Problem description

For s.value_counts(...) the result is

NaN     2
True    1
dtype: int64

but for s.mode(...) the output is:

0     NaN
1    True
2    <NA>
dtype: object

Expected Output

for the sake of consistancy one would expect mode to be only NaN, as it was counted twice with value_counts, i.e.

0     NaN
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : b5958ee
python : 3.7.3.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-53-generic
Version : #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.5
numpy : 1.19.2
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.0.0.post20201207
Cython : 0.29.21
pytest : 5.4.3
hypothesis : 5.36.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.2.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.50.1

@realead realead added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 6, 2021
@realead
Copy link
Contributor Author

realead commented Jan 6, 2021

The underlying issue is, that for value_counts with np.object, nas are always dropped:

{{if dtype == 'object'}}
build_count_table_{{dtype}}(values, table, 1)
{{else}}
build_count_table_{{dtype}}(values, table, dropna)
{{endif}}

and are taken care in a post processing step:

mask = isna(values)
if not dropna and mask.any():
if not isna(keys).any():
keys = np.insert(keys, 0, np.NaN)
counts = np.insert(counts, 0, mask.sum())

This is not done for mode:

build_count_table_{{dtype}}(values, table, dropna)

And because pd.NA and np.nan aren't "the same" for hashtables, we see three diffent modes.

@jreback jreback removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jan 24, 2021
@jreback jreback added this to the 1.3 milestone Jan 24, 2021
@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants