Skip to content

BUG: Sampling over selected groupbys does not reflect the selection #39928

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

Closed
2 of 3 tasks
wjsi opened this issue Feb 20, 2021 · 1 comment · Fixed by #40039
Closed
2 of 3 tasks

BUG: Sampling over selected groupbys does not reflect the selection #39928

wjsi opened this issue Feb 20, 2021 · 1 comment · Fixed by #40039
Labels
Bug Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@wjsi
Copy link
Contributor

wjsi commented Feb 20, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

>>> import pandas as pd
>>> df1 = pd.DataFrame({'a': [3, 5, 2, 7, 1, 2, 4, 6, 2, 4],
                       'b': [8, 3, 4, 1, 8, 2, 2, 2, 2, 3],
                       'c': [1, 8, 8, 5, 3, 5, 0, 0, 5, 4],
                       'd': [9, 7, 6, 3, 6, 3, 2, 1, 5, 8]}, index=list(range(10, 20)))
>>> # selecting one single column works
>>> df1.groupby('c').b.sample(1)
7    2
0    8
4    8
9    3
3    1
2    4
Name: b, dtype: int64
>>> # selecting multiple columns does not work
>>> df1.groupby('c')[['a', 'd']].sample(1)
   a  b  c  d
7  6  2  0  1
0  3  8  1  9
4  1  8  3  6
9  4  3  4  8
8  2  2  5  5
2  2  4  8  6

Problem description

When ['a', 'd'] is selected, the result should only contain column a and d. However, this selection has no effect on the result produced by groupby-sample.

Expected Output

>>> df1.groupby('c')[['a', 'd']].sample(1)
   a  d
7  6  1
0  3  9
4  1  6
9  4  8
8  2  5
2  2  6

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Tue Jan 12 22:13:05 PST 2021; root:xnu-6153.141.16~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : zh_CN.UTF-8
pandas : 1.2.2
numpy : 1.19.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : 0.29.21
pytest : 6.0.1
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : 0.4.2
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.18
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.51.2

@wjsi wjsi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 20, 2021
@lithomas1
Copy link
Member

@wjsi Thanks for the bug report! I can reproduce this on master. This problem seems to be limited to sample. For example when I do

>>> df1.groupby('c')[['a', 'd']].max()
   a  d
c
0  6  2
1  3  9
3  1  6
4  4  8
5  7  5
8  5  7

column selection works.

@lithomas1 lithomas1 added Groupby Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2021
@lithomas1 lithomas1 added this to the Contributions Welcome milestone Feb 24, 2021
@lithomas1 lithomas1 removed the Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff label Feb 25, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3 Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants