Skip to content

BUG: Calling dict(df.groupby(...)) raises TypeError: 'str' object is not callable despite valid inputs #61772

Open
@kay-ou

Description

@kay-ou

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 as pd

df = pd.DataFrame({
    'security': ['A', 'B'],
    'price': [1, 2]
})

print(dict(df.groupby('security')))  # ❌ Raises TypeError


Using a comprehension works fine:

res = {k: v for k, v in df.groupby('security')}  # ✅ Succeeds

Verifying the iteration:

for k, v in df.groupby('security'):
    print(type(k), type(v))  # <class 'str'>, <class 'DataFrame'>

Issue Description

When using the built-in dict() constructor on a DataFrameGroupBy object returned by pandas.DataFrame.groupby(...), I get:

TypeError: 'str' object is not callable

This occurs even though the iterable yields valid (str, DataFrame) pairs, and built-in dict is not shadowed.

Environment Info

Item Value
pandas version 2.3.0
Python version 3.12.3
Install method poetry
OS Ubuntu 22.04
Reproducible in venv ✅ Yes
Reproducible in clean script ✅ Yes

Additional Notes

  • dict is <class 'type'> and matches builtins.dict
  • Removing pycache and .pyc files does not help
  • The error only occurs when using dict(df.groupby(...)), not in other contexts
  • inspect.getmodule(dict) returns the expected built-in location

This could potentially be a pandas bug, interpreter-edge case, or a low-level compatibility glitch with Python 3.12+. Please let me know if you'd like a deeper trace or full traceback logs!

Expected Behavior

{
'A': pd.DataFrame(...),
'B': pd.DataFrame(...)
}

Installed Versions

Item Value
Python version 3.12.3
OS Ubuntu 22.04
pandas version 2.3.0
Install method poetry
Reproduced in venv Yes
Reproduced in CLI Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugGroupbyNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions