-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
value_counts unexpected behaviour - bins and dropna #25970
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
Comments
Here's the relevant section of code: pandas/pandas/core/algorithms.py Lines 685 to 696 in caad3b5
So it appears that the normalization scheme and the exclusion of nan in the index were intentional design decisions. Not sure of the original premise @jreback |
1st example In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: '0.25.0.dev0+359.gcaad3b5e5'
In [2]: s = pd.Series([0, 0, np.nan, 1, 2])
In [3]: s
Out[3]:
0 0.0
1 0.0
2 NaN
3 1.0
4 2.0
dtype: float64
In [4]: s.value_counts(normalize=True)
Out[4]:
0.0 0.50
2.0 0.25
1.0 0.25
dtype: float64 It seems like this should be consistent across the board regardless of 2nd example |
take |
This does seem inconsistent and still a problem in the latest version. I plan to alter to so that bin frequencies add up to 1 (when dropna=True). |
Testing this also revealed that SeriesGroupBy does value_counts inconsistently with Series. I'm not sure why these tests were passing before. I'll attempt to modify SeriesGroupBy.value_counts as well. Example: df[1].value_counts(dropna=False, normalize=True) |
Code Sample, a copy-pastable example if possible
1st example:
Input:
Output:
Expected output:
2nd example:
Input:
Output:
Expected Output:
Problem description
dropna argument in value_counts() seems to have no effect when bins is not None. Expected behaviour is better, because it sums up shares up to 1 when dropna=True and shows NaNs when dropna=False.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.24.2
pytest: None
pip: 19.0.3
setuptools: 40.8.0
Cython: None
numpy: 1.16.2
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: