Skip to content
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: pd.Series.rename(..., inplace=True) returns a pd.Series and not nont #60942

Closed
3 tasks done
loicdiridollou opened this issue Feb 16, 2025 · 3 comments · Fixed by #60975
Closed
3 tasks done

BUG: pd.Series.rename(..., inplace=True) returns a pd.Series and not nont #60942

loicdiridollou opened this issue Feb 16, 2025 · 3 comments · Fixed by #60975
Labels
Bug Docs good first issue inplace Relating to inplace parameter or equivalent rename .rename, .rename_axis

Comments

@loicdiridollou
Copy link
Contributor

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

In [1]: import pandas as pd

In [2]: pd.Series([1, 2, 3])
Out[2]:
0    1
1    2
2    3
dtype: int64

In [3]: pd.Series([1, 2, 3]).rename('A')
Out[3]:
0    1
1    2
2    3
Name: A, dtype: int64

In [4]: pd.Series([1, 2, 3]).rename('A', inplace=True). # should return None
Out[4]:
0    1
1    2
2    3
Name: A, dtype: int64

Issue Description

According to the documation, inplace operations should return None and only modify the object in place without doing a deepcopy.
However when running the rename operation on pd.Series with inplace=True the type of the returns is pd.Series and not None.
This seems to originate from quick look up in the _set_name operations that return and object no matter the value of inplace (cf

pandas/pandas/core/series.py

Lines 1835 to 1850 in 6bcd303

def _set_name(
self, name, inplace: bool = False, deep: bool | None = None
) -> Series:
"""
Set the Series name.
Parameters
----------
name : str
inplace : bool
Whether to modify `self` directly or return a copy.
"""
inplace = validate_bool_kwarg(inplace, "inplace")
ser = self if inplace else self.copy(deep=False)
ser.name = name
return ser
).
And then the rename operation only returns the result of _set_name without distinguishing the inplace value.

Expected Behavior

The docs suggest that the return of the rename(..., inplace=True) should be None.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.1
python-bits : 64
OS : Darwin
OS-release : 24.3.0
Version : Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:06 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8103
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.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.29.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.3
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

@loicdiridollou loicdiridollou added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 16, 2025
@rhshadrach
Copy link
Member

Thanks for the report. Currently this is an inconsistency, but we will eventually be changing this so that all inplace=True return the instance. In addition, the inplace argument will be removed from rename. So closing this issue.

Ref: #51466

@rhshadrach rhshadrach added inplace Relating to inplace parameter or equivalent and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 16, 2025
@loicdiridollou
Copy link
Contributor Author

Great thanks for the note @rhshadrach.
Are you interested in me changing the docs to reflect the new behavior? Happy to make a PR!
Will have the pandas-stubs adjusted accordingly when this comes out!

@rhshadrach rhshadrach reopened this Feb 19, 2025
@rhshadrach
Copy link
Member

@loicdiridollou - an update to the docs would be welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Docs good first issue inplace Relating to inplace parameter or equivalent rename .rename, .rename_axis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants