Skip to content
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

ValueError: Must produce aggregated value, if DataFrame contains a DataFrame as an element #39436

Open
Jogala opened this issue Jan 27, 2021 · 1 comment · May be fixed by #57706
Open

ValueError: Must produce aggregated value, if DataFrame contains a DataFrame as an element #39436

Jogala opened this issue Jan 27, 2021 · 1 comment · May be fixed by #57706
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@Jogala
Copy link

Jogala commented Jan 27, 2021

This minimal example assigns the column B the first value of each group:

df = pd.DataFrame(data=np.asarray([[1,1,1,2],[4,4,4,10]]).transpose(),columns = ['A','B'])
print(df)
dfagg = df.groupby('A').agg({'B' : lambda x : x.iloc[0]})
print(dfagg)

Hence

   A   B
0  1   4
1  1   4
2  1   4
3  2  10

Becomes:

A    
1   4
2  10

If I now modify the value of column B to an array of DataFrames

df1 = pd.DataFrame({'x' : [0,1], 'y' : [-1,-2]})
df2 = pd.DataFrame({'x' : [0,3], 'y' : [11,12]})

df = pd.DataFrame(data=np.asarray([[1,1,1,2],[df1,df1,df1,df2]],dtype=object).transpose(),columns = ['A','B'])
print(df)
dfagg = df.groupby('A').agg({'B' : lambda x : x.iloc[0]})
print(dfagg)

I get the error:

ValueError: Must produce aggregated value

It seems like this is a bug. There is no logic behind this behavior.
I also think that it did not raise an error in pandas like 2 years ago, when I used the code snippet the last time.
Currently using the newest version 1.2.1

@Jogala Jogala added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2021
@Jogala Jogala changed the title ValueError: Must produce aggregated value if DataFrame contains a DataFrame as an element ValueError: Must produce aggregated value, if DataFrame contains a DataFrame as an element Jan 27, 2021
@rhshadrach
Copy link
Member

Thanks for the report. Am I correct in saying that your expected output is a Series where the values are themselves DataFrames?

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map Groupby labels Jan 29, 2021
@mzeitlin11 mzeitlin11 added Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 5, 2021
@rhshadrach rhshadrach linked a pull request Mar 2, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants