-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: transform("cumcount")
returns a Series instead of a Dataframe.
#60551
Comments
thanks for the report! looks like a duplicate of #5608 ? |
Thanks for spotting it, I only checked among opened issues. I see that the issue #5608 is closed and quite a lot has changed in the pandas API since the original issue was opened, so it might still be worthy to reconsider that decision in order to align cumulative operations. |
sure, going to ping @rhshadrach on this one then (personally I don't think it's worth changing at this point) |
With it's current behavior, I agree with @MarcoGorelli that this is not worth changing. Namely, However should the df = pd.DataFrame({"a": [1, 1, 1, 2], "b": [1, np.nan, np.nan, np.nan]}).set_index("a")
print(df.groupby("a").cumcount())
# 1 0
# 1 1
# 1 2
# 2 0
# dtype: int64
print(df.groupby("a").count())
# b
# a
# 1 1
# 2 0 If we were to change @MarcoGorelli - curious if you think this would be more worth the change. |
Thanks! My initial feeling is that indeed if And then it could also return a dataframe |
Was implementing this and I noticed cumcount currently has an odd behavior where it does not really count the first row. This is as it is documented:
To me, this is not really counting - especially if we are to introduce
To get current behavior, |
Would like to get any thoughts on the approach above. cc @pandas-dev/pandas-core |
Sounds good, I just think the I think I'd advocate for just making a breaking change in 3.0 here - it would be a loud change anyway (as the type of the return object is completely changing from Series to DataFrame), and so is unlikely to silently catch anyone by surprise I remember discussing in #49912 about whether to make a breaking change or to introduce something like No objections to going with the |
As a maintainer of a large codebase, it's the opposite for me. Upgrading a version of a dependency and seeing a series of test failures (either an outright error or an unexpected result, possibly far from the line that is the root cause) is far more painful than a warning message that points to the exact line telling me what I have to do. Still, perhaps many users of pandas write code that only needs to work for a short time and do not maintain it across major versions. For this, I agree that I opened #60593 as a way to perhaps satisfy both cases. |
I'm on board with the behavior discussed here. For the deprecation/change path I tried to form an opinion and all I came up with was "I trust Richard and Marco" |
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
The aggregate operation
transform("cumcount")
returns a Series whether all other cumulative operations return a DataFrame.Expected Behavior
I would expect all cumulative operations to return a DataFrame.
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.11.1
python-bits : 64
OS : Darwin
OS-release : 24.1.0
Version : Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.2.3
numpy : 2.1.3
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : 8.30.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: