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 main branch of pandas.
Reproducible Example
importpandasaspddefgenerate_df() ->pd.DataFrame:
returnpd.DataFrame(
{
1: [1,3,5,7,9],
2:['a', 'b', 'c', 'd', 'e'],
'date': datetime(2023,2,1)
},
range(5)
)
defgenerate_df2() ->pd.DataFrame:
returnpd.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)
)
defmy_func(df: pd.DataFrame) ->pd.Series:
data= [1/len(df)] *len(df)
returnpd.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 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
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
The text was updated successfully, but these errors were encountered: