Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] 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
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
Comment From: simonjayhawkins
Thanks @kay-ou for the report.
https://docs.python.org/3/library/stdtypes.html#dict:~:text=If%20no%20positional,the%20new%20dictionary.
Yes, the python builtin dict will first look for and presumably call a keys method.
df.groupby("security").keys()
# TypeError: 'str' object is not callable
and indeed gives the same error. So it appears that the GroupBy object has a keys attribute instead of a keys method.
df.groupby("security").keys
# 'security'
As you noted, the GroupBy object is iterable and maybe a more compact workaround is therefore
dict(iter(df.groupby("security")))
# {'A': security price
# 0 A 1,
# 'B': security price
# 1 B 2}
instead of using the comprehension.
It appears from the documentation https://pandas.pydata.org/docs/reference/groupby.html that the keys attribute is not defined in the public api, so to fix this issue, it maybe as simple as renaming that attribute.
Comment From: rhshadrach
Though not explicitly public, if we are going to make a change to keys I think we should deprecate. Also, any groupby on a DataFrame with a keys column will still suffer a similar issue because df.groupby(...).keys will be an attribute.