Skip to content

BUG: Series.rename(scalar, copy=False) still copies data #52540

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4726,6 +4726,13 @@ def rename(
errors=errors,
)
else:
# since _set_name supports only inplace and not copy param
# we determine inplace based on copy (if inplace is False)
# 1) If inplace is True, the value of copy is ignored
# 2) If inplace is False/not supplied(default case), then
# inplace is determined based on copy (opposite of copy)
if not inplace:
inplace = not copy
return self._set_name(index, inplace=inplace)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the gist of the issue is that _set_name ( in the else portion ) doesn't support copy param and solely relies on inplace param.
Hence the copy param supplied to rename is just ignored in this case.

Some possible solutions here would be -

  1. In the else portion, determine inplace based on copy ( in the case where inplace is False ) and then invoke _set_name
  2. Support copy and inplace in _set_name and change its implementation similar to rename

I guess approach 1 might work/be better ( subject to review )


@Appender(
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,9 @@ def test_rename_copy_false(self, using_copy_on_write):
else:
assert ser[0] == shallow_copy[0]
assert ser[1] == shallow_copy[9]

def test_rename_scalar_copy_false(self):
# GH 52450
ser = Series([1])
shallow_copy = ser.rename("series_a", copy=False)
assert id(ser) == id(shallow_copy)