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: subset parameter of DataFrameGroupBy.value_counts has no effect #46383

Closed
3 tasks done
LucasG0 opened this issue Mar 15, 2022 · 4 comments · Fixed by #48164
Closed
3 tasks done

BUG: subset parameter of DataFrameGroupBy.value_counts has no effect #46383

LucasG0 opened this issue Mar 15, 2022 · 4 comments · Fixed by #48164
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Groupby
Milestone

Comments

@LucasG0
Copy link
Contributor

LucasG0 commented Mar 15, 2022

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

>>> df = pd.DataFrame({"c1": ["a", "b", "c"], "c2": ["x", "y", "y"]}, index=[0, 1, 1])
>>> df
  c1 c2
0  a  x
1  b  y
1  c  y
>>> df.groupby(level=0).value_counts(subset=["c2"])
   c1  c2
0  a   x     1
1  b   y     1
   c   y     1
dtype: int64                                                                                                             dtype: int64

Issue Description

subset has no effect. Within the code, there is only a check that subset and grouping keys do not intersect.

Expected Behavior

Keeps only subset columns for counting unique combinations and drop the other columns.

>>> df.groupby(level=0).value_counts(subset=["c2"])
   c2
0  x     1
1  y     2
dtype: int64

So it is consistent with DataFrame.value_counts:

>>> df.value_counts(subset=["c2"])
c2
y     2
x     1
dtype: int64

In practice, this is equivalent of using df.groupby(level=0)[["c2"]].value_counts().

Installed 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

@LucasG0 LucasG0 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 15, 2022
@mroeschke mroeschke added Groupby Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 17, 2022
@gpapadok
Copy link

take

@zhenqin77
Copy link

take

@weikhor
Copy link
Contributor

weikhor commented May 1, 2022

Sorry. I do not know there is already another pull request exists. I already close my PR.

@simonjayhawkins
Copy link
Member

Thanks @LucasG0 for the report.

DataFrameGroupBy.value_counts added in pandas-1.4.0 (#43564). cc @corriebar

adding a 1.4.x milestone as any fix could be considered a backport candidate.

contributions and PRs welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment