Skip to content

BUG: unexpected pivot after calling DataFrame.groupby('col').apply(func) #55070

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

Closed
2 of 3 tasks
CaioLC opened this issue Sep 8, 2023 · 1 comment
Closed
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Bug Duplicate Report Duplicate issue or pull request Groupby

Comments

@CaioLC
Copy link

CaioLC commented Sep 8, 2023

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 main branch of pandas.

Reproducible Example

import pandas as pd

def generate_df() -> pd.DataFrame:
    return pd.DataFrame(
        {
            1: [1,3,5,7,9],
            2:['a', 'b', 'c', 'd', 'e'],
            'date': datetime(2023,2,1)
        },
        range(5)
    )
def generate_df2() -> pd.DataFrame:
    return pd.DataFrame(
        {
            1: [1,3,5,7,9, 10],
            2:['a', 'b', 'c', 'd', 'e', 'f'],
            'date': [datetime(2023,2,1),datetime(2023,2,1),datetime(2023,2,1),datetime(2023,2,2),datetime(2023,2,2),datetime(2023,2,2)]
        },
        range(6)
    )

def my_func(df: pd.DataFrame) -> pd.Series:
    data = [1/len(df)] * len(df)
    return pd.Series(data, df.index)

df1 = generate_df()
df2 = generate_df2()

res_unexpected = df1.groupby('date').apply(my_func) # unexpected pivoted table (index is now the column)
res2 = df2.groupby('date').apply(my_func) # this has the expected behavior. "my_func" returns a series, so the result should indeed be a series with a multiindex.

Issue Description

I have a function that accepts a dataframe and returns a pd.Series. When utilizing that inside a groupby.apply() I get different table formats depending on the GroupBy object. In the example above, if len(df['date'].unique()) == 1, the result is a DataFrame where "date" is the only index and the previous indices were pivoted into columns. If len(df['date'].unique()) > 1 I get the expected result of my custom function which is a pd.Series and the indices are 'date' and the previous index.

Expected Behavior

Output format should not vary depending on the number of unique values in a DataFrame groupby. I know that a groupby of 1 group doesn't make much sense, but it shouldn't be necessary to check that preemptively (in my specific we'll only know the unique values at runtime, so I had to add an if-else statement to handle both cases).

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.10.11.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : AMD64 Family 23 Model 1 Stepping 1, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.1.0
numpy : 1.24.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.1.2
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : 6.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : 2.0.20
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@CaioLC CaioLC added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 8, 2023
@rhshadrach
Copy link
Member

Thanks for the report! Closing as a duplicate of #54992.

@rhshadrach rhshadrach added Groupby Duplicate Report Duplicate issue or pull request Apply Apply, Aggregate, Transform, Map and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 9, 2023
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 Duplicate Report Duplicate issue or pull request Groupby
Projects
None yet
Development

No branches or pull requests

2 participants