-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: SeriesGroupBy.value_counts - index name missing when applied on categorical column #44324
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
take |
I found this minimal example In [1]: import pandas as pd
...:
...: df = pd.DataFrame(
...: {
...: "gender": ["female"],
...: "country": ["US"],
...: }
...: )
...:
...: print(df.groupby("country")["gender"].value_counts())
...: df["gender"] = df["gender"].astype("category")
...: print("*" * 60)
...: print(df.groupby("country")["gender"].value_counts())
country gender
US female 1
Name: gender, dtype: int64
************************************************************
country
US female 1
Name: gender, dtype: int64 |
In particular the problem is at the function In PR #38796 the following code has been introduced:[ pandas/pandas/core/groupby/generic.py Lines 614 to 621 in 011f116
If I simply delete this code the function will solve our problem. Producing the following output: In [1]: import pandas as pd
...:
...: df = pd.DataFrame(
...: {
...: "gender": ["female"],
...: "country": ["US"],
...: }
...: )
...:
...: print(df.groupby("country")["gender"].value_counts())
...: df["gender"] = df["gender"].astype("category")
...: print("*" * 60)
...: print(df.groupby("country")["gender"].value_counts())
country gender
US female 1
Name: gender, dtype: int64
************************************************************
country gender <-------------------------------------------
US female 1
Name: gender, dtype: int64 However we would fall back into the problem solved at PR #38796. I will investigate further solutions. |
…olumns (#45625) * BUG: SeriesGroupBy.value_counts index name missing Issue #44324 * TST: Change test to correct categorical naming Value counts tend to preserve index names #45625 Change test test_sorting_with_different_categoricals to comply to this change * REF: Refactor conditionals in value_counts() * RFT: correct mistake introduced via RFT In line with 44324 * RFT: Change variable names and comment #38672 * BUG: Update conditional to is None to consider series
…olumns (pandas-dev#45625) * BUG: SeriesGroupBy.value_counts index name missing Issue pandas-dev#44324 * TST: Change test to correct categorical naming Value counts tend to preserve index names pandas-dev#45625 Change test test_sorting_with_different_categoricals to comply to this change * REF: Refactor conditionals in value_counts() * RFT: correct mistake introduced via RFT In line with 44324 * RFT: Change variable names and comment pandas-dev#38672 * BUG: Update conditional to is None to consider series
…olumns (pandas-dev#45625) * BUG: SeriesGroupBy.value_counts index name missing Issue pandas-dev#44324 * TST: Change test to correct categorical naming Value counts tend to preserve index names pandas-dev#45625 Change test test_sorting_with_different_categoricals to comply to this change * REF: Refactor conditionals in value_counts() * RFT: correct mistake introduced via RFT In line with 44324 * RFT: Change variable names and comment pandas-dev#38672 * BUG: Update conditional to is None to consider series
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
Now, if using a categorical type on the genre column, the column name is not saved in the index:
Issue Description
The column name (here gender) is dropped when applying
value_counts()
on a column of typecategory
.It might be link with other problems in handling categorical types: #44001
This was working as of pandas v 1.2.3, and I could reproduce the bug starting with v 1.3.0, still present in v 1.3.4
Expected Behavior
Column name should be added in the index for uniformity with other types, as in the first example above with object type column.
Installed Versions
pandas : 1.3.4
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 57.0.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.26.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : 0.19.0
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: