BUG: value_counts(observed=True) inconsistent between SeriesGroupBy and DataFrameGroupBy #46357
Closed
2 of 3 tasks
Labels
Milestone
Uh oh!
There was an error while loading. Please reload this page.
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
Issue Description
The result contains
0
values for unused categories in each group withobserved=True
, unlikeDataFrameGroupBy
:Expected Behavior
The behavior should be the same for
SeriesGroupBy
andDataFrameGroupBy
. I am not sure what should be the expected behavior here.The
groupby
documentation specifies thatobserved
parameter has effect on groupers. Consideringby
andlevel
parameters are the groupers, it might make sense that in this caseGroupBy.value_counts
is not affected by theobserved
keyword and it follows the behavior ofSeries.value_counts
which does keep unused categories. It would mean thatGroupBy.value_counts
(and probablySeries/DataFrame.value_counts
too) would require an additionalobserved
parameter, like the suggestion in #43498.On the other hand, if we consider that the
observed
parameter makes sense in this case,SeriesGroupBy.value_counts(observed=True)
should not return0
values for unused categories as forDataFrameGroupBy
.Related: performance issue on
GroupBy.value_counts
on categoricals #46202Installed Versions
INSTALLED VERSIONS
commit : 06d2301
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 11, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : fr_FR.cp1252
pandas : 1.4.1
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.3
setuptools : 52.0.0.post20210125
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.1.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
The text was updated successfully, but these errors were encountered: