BUG: Sampling over selected groupbys does not reflect the selection · Issue #39928 · pandas-dev/pandas (original) (raw)
- 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