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 behaviour for DataFrame.value_counts and Series.value_counts on categoricals #44001

Open
2 of 3 tasks
corriebar opened this issue Oct 12, 2021 · 3 comments
Open
2 of 3 tasks
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Categorical Categorical Data Type

Comments

@corriebar
Copy link
Contributor

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd

df = (pd.DataFrame({'gender' : ['male', 'female', 'male', 'female', 'male'],
                  'country': ['BR', 'BR', 'US', 'BR', 'US']})
      .astype('category') 
     )
df.value_counts() # drops all empty categories
# gender  country
# female  BR         2
# male    US         2
#         BR         1
# dtype: int64

# also compare
(df
 .query("country != 'BR'")
 .value_counts()
) # drops non-observed categories
# gender  country
# male    US         2
# dtype: int64

(df
 .query("country != 'BR'")
 ['gender']
 .value_counts()
) # shows 0 for non-observed catgories
# male      2
# female    0
# Name: gender, dtype: int64

Issue Description

DataFrame.value_counts() drops any non observed categories while Series.value_counts() shows 0 for non-observed categories.

There was some discussion about the zeroes for Series.value_counts() in this issue #8559, based on that I checked behaviour in R which also keeps the zeros:

  gender <- factor(c('male', 'female', 'male', 'female', 'male'))
  country <- factor( c( 'BR', 'BR', 'US', 'BR', 'US') )
df <- data.frame(gender=gender, country=country)
table(df)
#>         country
#> gender   BR US
#>   female  2  0
#>   male    1  2

Expected Behavior

The following work-around gives the expected behaviour:

df.groupby("gender").country.value_counts()
# gender    
# female  BR    2
#         US    0
# male    US    2
#         BR    1
# Name: country, dtype: int64

This fails however for more than two categorical variables.

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Sep 16 20:58:47 PDT 2021; root:xnu-6153.141.40.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
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 : 7.27.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@corriebar corriebar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 12, 2021
@rhshadrach rhshadrach added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Categorical Categorical Data Type labels Oct 16, 2021
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 16, 2021
@NumberPiOso
Copy link
Contributor

take

@NumberPiOso
Copy link
Contributor

We were following an outdated issue. The expected behavior changed dramatically.
PR #20583 API: categorical grouping will no longer return the cartesian product
Due to some BUGs as in #14942.

With that said, I think this issue should be closed.

@rhshadrach
Copy link
Member

The title to #20583 is no longer accurate; that PR ended up adding the observed keyword to groupby instead. This issue is still relevant.

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 Categorical Categorical Data Type
Projects
None yet
Development

No branches or pull requests

4 participants