diff --git a/doc/source/whatsnew/v2.0.2.rst b/doc/source/whatsnew/v2.0.2.rst index e791e2a58ba5b..2b07622d70c3f 100644 --- a/doc/source/whatsnew/v2.0.2.rst +++ b/doc/source/whatsnew/v2.0.2.rst @@ -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`) - diff --git a/pandas/core/series.py b/pandas/core/series.py index 29e02fdc7695d..78f4da4e65196 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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. @@ -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 @@ -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", @@ -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( """ diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 1d8f1dea7d478..c220c46bdc8f8 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -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], @@ -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",