Skip to content

Commit

Permalink
Backport PR #53189 on branch 2.0.x (BUG/CoW: Series.rename not making…
Browse files Browse the repository at this point in the history
… a lazy copy when passed a scalar) (#53221)

* Backport PR #53189: BUG/CoW: Series.rename not making a lazy copy when passed a scalar

* Update test_setitem.py

---------

Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and lithomas1 committed May 14, 2023
1 parent 895b350 commit cc47ec2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Bug fixes
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)
- Bug in :meth:`Series.rename` not making a lazy copy when Copy-on-Write is enabled when a scalar is passed to it (:issue:`52450`)
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
-
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,9 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
df = self._constructor_expanddim(mgr)
return df.__finalize__(self, method="to_frame")

def _set_name(self, name, inplace: bool = False) -> Series:
def _set_name(
self, name, inplace: bool = False, deep: bool | None = None
) -> Series:
"""
Set the Series name.
Expand All @@ -1949,9 +1951,11 @@ def _set_name(self, name, inplace: bool = False) -> Series:
name : str
inplace : bool
Whether to modify `self` directly or return a copy.
deep : bool|None, default None
Whether to do a deep copy, a shallow copy, or Copy on Write(None)
"""
inplace = validate_bool_kwarg(inplace, "inplace")
ser = self if inplace else self.copy()
ser = self if inplace else self.copy(deep and not using_copy_on_write())
ser.name = name
return ser

Expand Down Expand Up @@ -4770,7 +4774,7 @@ def rename(
index: Renamer | Hashable | None = None,
*,
axis: Axis | None = None,
copy: bool = True,
copy: bool | None = None,
inplace: bool = False,
level: Level | None = None,
errors: IgnoreRaise = "ignore",
Expand Down Expand Up @@ -4857,7 +4861,7 @@ def rename(
errors=errors,
)
else:
return self._set_name(index, inplace=inplace)
return self._set_name(index, inplace=inplace, deep=copy)

@Appender(
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def test_methods_copy_keyword(
"method",
[
lambda ser, copy: ser.rename(index={0: 100}, copy=copy),
lambda ser, copy: ser.rename(None, copy=copy),
lambda ser, copy: ser.reindex(index=ser.index, copy=copy),
lambda ser, copy: ser.reindex_like(ser, copy=copy),
lambda ser, copy: ser.align(ser, copy=copy)[0],
Expand All @@ -152,6 +153,7 @@ def test_methods_copy_keyword(
lambda ser, copy: ser.set_flags(allows_duplicate_labels=False, copy=copy),
],
ids=[
"rename (dict)",
"rename",
"reindex",
"reindex_like",
Expand Down

0 comments on commit cc47ec2

Please sign in to comment.