You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importnumpyasnpimportpandas# Could be any data with MultiIndex columnsdata= {(f"col{i}", f"level-{i%2}"): np.arange(5) foriinrange(5)}
df=pandas.DataFrame(data)
by=df.columns[0]
agg_col=df.columns[1]
agg_dict= {agg_col: ["min", "max"]}
df.groupby(by, as_index=False).agg(func=agg_dict) # ValueError
Traceback
Traceback (most recent call last):
File "../exp.py", line 12, in <module>
df.groupby(by, as_index=False).agg(func=agg_dict) # ValueError
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 990, in aggregate
self._insert_inaxis_grouper_inplace(result)
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 1614, in _insert_inaxis_grouper_inplace
result.insert(0, name, lev)
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/frame.py", line 3760, in insert
self._mgr.insert(loc, column, value, allow_duplicates=allow_duplicates)
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 1197, in insert
new_axis = self.items.insert(loc, item)
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 3801, in insert
item = self._validate_fill_value(item)
File "/localdisk/dchigare/miniconda3/envs/modin_tests/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 3784, in _validate_fill_value
raise ValueError("Item must have length equal to number of levels.")
ValueError: Item must have length equal to number of levels.
Problem description
When we specify an aggregation function via list, pandas adds a new level with function name to the columns of the resulted frame:
In the result, the number of levels in the index which represents grouper and columns is different, which causes some problems in _insert_inaxis_grouper_inplace. When we're trying to insert MultiIndex grouper inside the resulted frame, but their nlevels is different, pandas.DataFrame.insert raises a ValueError because of it.
Filling mismatched levels with an empty string (like pandas already does when inserted value don't have MultiIndex columns) will probably solve this issue
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
Traceback
Problem description
When we specify an aggregation function via list, pandas adds a new level with function name to the columns of the resulted frame:
In the result, the number of levels in the index which represents grouper and columns is different, which causes some problems in
_insert_inaxis_grouper_inplace
. When we're trying to insert MultiIndex grouper inside the resulted frame, but theirnlevels
is different,pandas.DataFrame.insert
raises aValueError
because of it.Filling mismatched levels with an empty string (like pandas already does when inserted value don't have MultiIndex columns) will probably solve this issue
Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: