Skip to content

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

Closed
@LucasG0

Description

@LucasG0

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

Metadata

Metadata

Assignees

Labels

AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBugGroupby

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions