BUG: column selection in GroupBy NOT returning tuple when grouping by a list containing single element · Issue #53500 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd df = pd.DataFrame({"a": [1,1,1,2,2], "b": [4,5,6,4,4], "c":[7,7,8,9,9]})

print("Each k is a tuple as expected") for k, v in df.groupby(["a"]): print(type(k), k) print(v)

print() print("Each k is an int, and it's not expected") for k, v in df.groupby(["a"])[["a", "b", "c"]]: print(type(k), k) print(v)

Result:

Each k is a tuple as expected
<class 'tuple'> (1,)
   a  b  c
0  1  4  7
1  1  5  7
2  1  6  8
<class 'tuple'> (2,)
   a  b  c
3  2  4  9
4  2  4  9

Each k is an int, and it's not expected
<class 'int'> 1
   a  b  c
0  1  4  7
1  1  5  7
2  1  6  8
<class 'int'> 2
   a  b  c
3  2  4  9
4  2  4  9

Issue Description

After the change in #47761, when grouping by a list containing a single element, a tuple of one element is returned.

However, if a column selection is specified after groupby, a tuple is not returned (an int is returned in the previous example).

Expected Behavior

For consistency, I would expect that a tuple is returned also in the second case.

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.10.8.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.42.2.el7.x86_64
Version : #1 SMP Tue Aug 31 20:15:00 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.2
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.11.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.7
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None