Skip to content

BUG: groupby on columns does not work with Series #33842

@FlorentinGerard

Description

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

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


No groups are found when using a pd.Series instead of a list in a groupby(axis=1)

>>> df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=['A', 'B', 'C', 'D'])

>>> grouped_list_row = df.groupby([1, 2, 1, 2])
>>> print(f"number of elem list_row: {len(grouped_list_row)}")

>>> grouped_series_row = df.groupby(pd.Series([1, 2, 1, 2]))
>>> print(f"number of elem series_row: {len(grouped_series_row)}")

>>> grouped_list_column = df.groupby([1, 2, 1, 2], axis=1)
>>> print(f"number of elem list_column: {len(grouped_list_column)}")

>>> grouped_series_column = df.groupby(pd.Series([1, 2, 1, 2]), axis=1)
>>> print(f"number of elem series_column: {len(grouped_series_column)}")

number of elem list_row: 2
number of elem series_row: 2
number of elem list_column: 2
number of elem series_column: 0

Problem description

I would expect to have the same result with a list and with a pd.Series(list).
Instead no groups are created

Expected Output

number of elem list_row: 2
number of elem series_row: 2
number of elem list_column: 2
number of elem series_column: 2

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.4.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel byteorder : little LC_ALL : None LANG : fr_FR.UTF-8 LOCALE : None.None

pandas : 1.0.3
numpy : 1.17.1
pytz : 2019.2
dateutil : 2.8.0
pip : 18.1
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.7.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions