Skip to content

BUG: dictionary groupby aggregation fails in case there're duplicated values in MultiIndex columns #50402

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

Open
2 of 3 tasks
dchigarev opened this issue Dec 22, 2022 · 1 comment

Comments

@dchigarev
Copy link

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

import pandas

df = pandas.DataFrame(
    [[1, 2, 3], [4, 5, 6]],
    columns=pandas.MultiIndex.from_tuples([("a", 0), ("b", 0), ("b", 1)]),
)

res = df.groupby(by=[1, 2]).agg({("a", 0): "sum"})  # works fine
print(res)

df.columns = pandas.MultiIndex.from_tuples([("a", 0), ("b", 0), ("b", 0)])
res = df.groupby(by=[1, 2]).agg(
    {("a", 0): "sum"}
)  # AttributeError: 'Series' object has no attribute 'columns'
print(res)

Issue Description

Reproducer's traceback
Traceback (most recent call last):
  File "t2.py", line 12, in <module>
    res = df.groupby(by=[1, 2]).agg({("a", 0): "sum"}) # AttributeError: 'Series' object has no attribute 'columns'
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/generic.py", line 894, in aggregate
    result = op.agg()
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/apply.py", line 169, in agg
    return self.agg_dict_like()
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/apply.py", line 486, in agg_dict_like
    results = {
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/apply.py", line 487, in <dictcomp>
    key: obj._gotitem(key, ndim=1).agg(how) for key, how in arg.items()
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/generic.py", line 275, in aggregate
    return getattr(self, func)(*args, **kwargs)
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 2425, in sum
    result = self._agg_general(
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 1683, in _agg_general
    result = self._cython_agg_general(
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 1743, in _cython_agg_general
    numeric_only_bool = self._resolve_numeric_only(how, numeric_only, axis=0)
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 1347, in _resolve_numeric_only
    if len(obj.columns) and not len(check.columns) and not obj.empty:
  File "/localdisk/dchigare/miniconda3/envs/modin/lib/python3.8/site-packages/pandas/core/generic.py", line 5902, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'columns'

Expected Behavior

Groupby on a frame with duplicated non-MultiIndex values works fine, I would expect the MultiIndex case to work the same.

import pandas

df = pandas.DataFrame(
    [[1, 2, 3], [4, 5, 6]],
    columns=pandas.Index(["a", "b", "b"]),
)

res = df.groupby(by=[1, 2]).agg({"a": "sum"})  # works fine
print(res)

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-124-generic
Version : #140-Ubuntu SMP Thu Aug 4 02:23:37 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.23.3
pytz : 2022.4
dateutil : 2.8.2
setuptools : 65.4.1
pip : 22.2.2
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : 5.3.0
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli :
fastparquet : 0.8.3
fsspec : 2022.8.2
gcsfs : None
matplotlib : 3.3.0
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.9
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.8.2
scipy : 1.9.1
snappy : None
sqlalchemy : 1.4.41
tables : 3.7.0
tabulate : None
xarray : 2022.9.0
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

@dchigarev dchigarev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 22, 2022
@dchigarev dchigarev changed the title BUG: dictionary groupby aggregation fails in case duplicated MultiIndex values are in columns BUG: dictionary groupby aggregation fails in case there're duplicated values in MultiIndex columns Dec 22, 2022
@rhshadrach
Copy link
Member

Thanks for the report! Likely this is due to differences in results when a MultiIndex has duplicates in DataFrame:

df = pd.DataFrame({'a': [1, 1, 2, 2], 'b': [3, 4, 5, 5], 'c': range(4)}).set_index(['a', 'b'])
print(df.loc[1, 3])
#      c
# a b   
# 1 3  0

df = pd.DataFrame({'a': [1, 1, 2, 2], 'b': [3, 4, 5, 6], 'c': range(4)}).set_index(['a', 'b'])
print(df.loc[1, 3])
# c    0
# Name: (1, 3), dtype: int64

Agreed this behavior should be handled internally. Further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants