Skip to content

BUG: pandas groupby returns different results when one and two groups #58133

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
3 tasks done
ibigquant opened this issue Apr 3, 2024 · 2 comments
Closed
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ibigquant
Copy link

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

# df = pd.DataFrame({"a": [1, 1, 1, 1], "b": [1, 2, 3, 4]})
df = pd.DataFrame({"a": [1, 2, 2, 1], "b": [1, 2, 3, 4]})

def foo(xf):
    def bar(x):
        # here just some sample code ..
        return x['b'] + 1

    return xf.apply(bar, axis=1)

result_series = df.groupby("a", group_keys=False).apply(foo)

result_series

Issue Description

it returns dataframe when there's only one group

image

Expected Behavior

always returns series

image

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.11.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.14.0-162.23.1.el9_1.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Tue Apr 11 19:09:37 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : zh_CN.UTF-8
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 65.5.0
pip : 24.0
Cython : 3.0.9
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.2
pandas_datareader : 0.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : 0.59.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : 2.0.29
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@ibigquant ibigquant added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 3, 2024
@shriyakalakata
Copy link
Contributor

I have run the example above and it returned the expected behavior.

My output:

0    2
3    5
1    3
2    4
dtype: int64

Pandas version: 2.2.1

@rhshadrach
Copy link
Member

Thanks for the report, closing as a duplicate of #54992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants