Skip to content

Commit cc47ec2

Browse files
Backport PR #53189 on branch 2.0.x (BUG/CoW: Series.rename not making 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 <[email protected]>
1 parent 895b350 commit cc47ec2

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

doc/source/whatsnew/v2.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Bug fixes
3030
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
3131
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
3232
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)
33+
- 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`)
3334
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)
3435
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
3536
-

pandas/core/series.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,9 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
19401940
df = self._constructor_expanddim(mgr)
19411941
return df.__finalize__(self, method="to_frame")
19421942

1943-
def _set_name(self, name, inplace: bool = False) -> Series:
1943+
def _set_name(
1944+
self, name, inplace: bool = False, deep: bool | None = None
1945+
) -> Series:
19441946
"""
19451947
Set the Series name.
19461948
@@ -1949,9 +1951,11 @@ def _set_name(self, name, inplace: bool = False) -> Series:
19491951
name : str
19501952
inplace : bool
19511953
Whether to modify `self` directly or return a copy.
1954+
deep : bool|None, default None
1955+
Whether to do a deep copy, a shallow copy, or Copy on Write(None)
19521956
"""
19531957
inplace = validate_bool_kwarg(inplace, "inplace")
1954-
ser = self if inplace else self.copy()
1958+
ser = self if inplace else self.copy(deep and not using_copy_on_write())
19551959
ser.name = name
19561960
return ser
19571961

@@ -4770,7 +4774,7 @@ def rename(
47704774
index: Renamer | Hashable | None = None,
47714775
*,
47724776
axis: Axis | None = None,
4773-
copy: bool = True,
4777+
copy: bool | None = None,
47744778
inplace: bool = False,
47754779
level: Level | None = None,
47764780
errors: IgnoreRaise = "ignore",
@@ -4857,7 +4861,7 @@ def rename(
48574861
errors=errors,
48584862
)
48594863
else:
4860-
return self._set_name(index, inplace=inplace)
4864+
return self._set_name(index, inplace=inplace, deep=copy)
48614865

48624866
@Appender(
48634867
"""

pandas/tests/copy_view/test_methods.py

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def test_methods_copy_keyword(
135135
"method",
136136
[
137137
lambda ser, copy: ser.rename(index={0: 100}, copy=copy),
138+
lambda ser, copy: ser.rename(None, copy=copy),
138139
lambda ser, copy: ser.reindex(index=ser.index, copy=copy),
139140
lambda ser, copy: ser.reindex_like(ser, copy=copy),
140141
lambda ser, copy: ser.align(ser, copy=copy)[0],
@@ -152,6 +153,7 @@ def test_methods_copy_keyword(
152153
lambda ser, copy: ser.set_flags(allows_duplicate_labels=False, copy=copy),
153154
],
154155
ids=[
156+
"rename (dict)",
155157
"rename",
156158
"reindex",
157159
"reindex_like",

0 commit comments

Comments
 (0)