Skip to content

BUG: TypeError when trying to group on column name and index level at the same time #40378

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
dchigarev opened this issue Mar 11, 2021 · 2 comments · Fixed by #47778 or #47825
Closed
2 of 3 tasks
Labels
Docs Error Reporting Incorrect or improved errors from pandas Groupby

Comments

@dchigarev
Copy link

  • 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

df = pandas.DataFrame({"col1": ["a", "b", "c"]})
df.groupby("col1", level=0)  # Fine

df.index = pandas.MultiIndex.from_arrays([["a", "a", "b"], [1, 2, 1]], names=["x", "y"])

df.groupby(["col1", "x"]) # Fine
df.groupby("col1", level=0)  # TypeError
Traceback
Traceback (most recent call last):
  File "rofl.py", line 7, in <module>
    res = df.groupby("col1", level=0) # TypeError
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/frame.py", line 6727, in groupby
    dropna=dropna,
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 568, in __init__
    dropna=self.dropna,
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/groupby/grouper.py", line 839, in get_grouper
    if not isinstance(gpr, Grouping)
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/groupby/grouper.py", line 485, in __init__
    ) = index._get_grouper_for_level(self.grouper, level)
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 1487, in _get_grouper_for_level
    grouper = level_values.map(mapper)
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 5098, in map
    new_values = super()._map_values(mapper, na_action=na_action)
  File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/base.py", line 937, in _map_values
    new_values = map_f(values, mapper)
  File "pandas/_libs/lib.pyx", line 2467, in pandas._libs.lib.map_infer
TypeError: 'numpy.ndarray' object is not callable

Problem description

Getting a TypeError when specifying by and level params in .groupby() at the same time. It seems that grouping on column name and index level is supported since there is no error when level name is located in by parameter, so specifying it with level should also be possible.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : f2c8480af2f25efdbd803218b9d87980f416563e
python           : 3.7.7.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.4.0-65-generic
Version          : #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.3
numpy            : 1.19.0
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 47.3.1.post20200622
Cython           : None
pytest           : 6.0.2
hypothesis       : None
sphinx           : None
blosc            : None
feather          : 0.4.1
xlsxwriter       : None
lxml.etree       : 4.5.1
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : None
pandas_datareader: None
bs4              : 4.9.1
bottleneck       : None
fsspec           : 0.7.4
fastparquet      : None
gcsfs            : None
matplotlib       : 3.2.2
numexpr          : 2.7.1
odfpy            : None
openpyxl         : 3.0.4
pandas_gbq       : 0.13.2
pyarrow          : 1.0.1
pyxlsb           : None
s3fs             : 0.4.2
scipy            : 1.5.1
sqlalchemy       : 1.3.18
tables           : 3.6.1
tabulate         : None
xarray           : 0.15.1
xlrd             : 1.2.0
xlwt             : None
numba            : None

@dchigarev dchigarev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 11, 2021
@rhshadrach
Copy link
Member

rhshadrach commented Mar 12, 2021

Doing

df = DataFrame({"col1": [1, 2, 3]}, index=[1, 1, 2])
print(df.groupby(level=0).sum())
print(df.groupby("col1", level=0).sum())

each line produces the same output, so while the groupby construction works, the result is not correct. I don't see a compelling reason this should be supported. Instead, it seems to me the docstring should be updated to specify that both by and level should not be specified, and the corresponding argument validation should be done in groupby.

@rhshadrach rhshadrach added Groupby Docs Error Reporting Incorrect or improved errors from pandas and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 12, 2021
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Mar 12, 2021
@GivyBoy
Copy link
Contributor

GivyBoy commented Jul 12, 2022

Can this issue be assigned to me? I have already started to work on it. Thanks!

GivyBoy added a commit to GivyBoy/pandas-agg that referenced this issue Jul 31, 2022
mroeschke pushed a commit that referenced this issue Aug 8, 2022
…roupby`` - GH40378 (#47825)

Added that ``level`` and ``by`` should not be specified when using the ``groupby`` function (#40378)
noatamir pushed a commit to noatamir/pandas that referenced this issue Nov 9, 2022
…roupby`` - GH40378 (pandas-dev#47825)

Added that ``level`` and ``by`` should not be specified when using the ``groupby`` function (pandas-dev#40378)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment