Open truhanen opened 3 years ago
You would have to explicitly use iter
with GroupBy
object.
import pandas as pd
frame = pd.DataFrame([[1, 2], [3, 4]])
d = dict(iter(frame.groupby(level=0)))
d[0]
0 1
0 1 2
d[1]
0 1
1 3 4
With iter
you create an iterator from an iterable. GroupBy
is an iterable because it implements the __iter__
method, and should therefore be directly compatible for dict
initialization, as stated in the documentation I linked.
Of course there's a possibility that I have understood the documentation incorrectly.
take
Python seems to be inferring that the groupby object is a Mapping, but I'm not sure why this is. I tried removing __getitem__
and .keys
from the DataFrameGroupBy class and it still seems to be treating it as a Mapping.
When I use frame.groupby("a")
, the error is TypeError: 'str' object is not callable
. When I delete keys
, the error becomes TypeError: 'NoneType' object is not callable
.
Code Sample, a copy-pastable example
Raised error:
Problem description
Based on the documentation of Python
dict
&GroupBy.__iter__
, the two should work together. The above code should behave identically to the working alternativeExpected Output
The code sample shouldn't raise an error, and the result should be
Output of
pd.show_versions()