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.
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
importpandasaspddf=pd.DataFrame({'A': 'a a'.split(),
'B': [1,2],
'C': [4,6]})
df.groupby("A").apply(lambdax: x["C"]>x["B"])
# output# 0 1# A # a True True# dtype: booldf2=pd.DataFrame({'A': 'a a b b'.split(),
'B': [1,2, 3, 4],
'C': [5,6, 7, 8],
'D': [5,6, 7, 8]
}
)
df2.groupby("A").apply(lambdax: x["C"]>x["B"])
# output # A # a 0 True# 1 True# b 2 True# 3 True# dtype: bool
Issue Description
the apply function is returning a series. if a single group is output the series is treated as a row vector, if multiple groups it is returned as a column vector as expected.
Expected Behavior
df.groupby("A").apply(lambdax: x["C"]>x["B"])
# output # A # a 0 True# 1 True
Installed Versions
python 3.10.1
pandas 1.3.5
show versions crashed (created by running conda create -n test_pandas pandas on Mac OSX)
The text was updated successfully, but these errors were encountered:
I have confirmed this bug exists on the master branch of pandas (I was going to report this issue when I noticed this report).
I would add that in the case with one group the result's type is a DataFrame which can cause issues on its own if a Series is expected.
My current workaround is applying squeeze(axis=0) to the result of apply in the case of one group.
The issue is with all_indexed_same in pandas.core.groupby.generic._wrap_applied_output_series. We try to determine how the output should be stacked. If the results are all indexed the same it is usually because the operation is a reducer producing a Series where the index is the columns of the input frame. In such a case, we want to turn the Series into a row of the result rather than stacking it vertically. However, if the indexes in the results are not all the same, we stack them vertically (the desired behavior here).
Trouble is, when there is only one group, the indices of course are all the same. I'm not seeing a better way of determining how to wrap the result, but currently still searching.
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 master branch of pandas.
Reproducible Example
Issue Description
the apply function is returning a series. if a single group is output the series is treated as a row vector, if multiple groups it is returned as a column vector as expected.
Expected Behavior
Installed Versions
python 3.10.1
pandas 1.3.5
show versions crashed (created by running conda create -n test_pandas pandas on Mac OSX)
The text was updated successfully, but these errors were encountered: