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

DEPR: Deprecate remaining copy usages #57870

Merged
merged 6 commits into from
Mar 26, 2024
Merged
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 doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ will be removed in a future version:
- :meth:`DataFrame.astype` / :meth:`Series.astype`
- :meth:`DataFrame.reindex` / :meth:`Series.reindex`
- :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like`
- :meth:`DataFrame.set_axis` / :meth:`Series.set_axis`
- :meth:`DataFrame.to_period` / :meth:`Series.to_period`
- :meth:`DataFrame.to_timestamp` / :meth:`Series.to_timestamp`
- :meth:`DataFrame.rename` / :meth:`Series.rename`
- :meth:`DataFrame.transpose`
- :meth:`DataFrame.swaplevel`
- :meth:`DataFrame.merge` / :func:`pd.merge`

Copy-on-Write utilizes a lazy copy mechanism that defers copying the data until
necessary. Use ``.copy`` to trigger an eager copy. The copy keyword has no effect
Expand Down
51 changes: 37 additions & 14 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
of a string to indicate that the column name from `left` or
`right` should be left as-is, with no suffix. At least one of the
values must not be None.
copy : bool, default True
copy : bool, default False
If False, avoid copy if possible.

.. note::
Expand All @@ -371,6 +371,8 @@

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0
indicator : bool or str, default False
If True, adds a column to the output DataFrame called "_merge" with
information on the source of each row. The column can be given a different
Expand Down Expand Up @@ -3576,7 +3578,11 @@ def memory_usage(self, index: bool = True, deep: bool = False) -> Series:
result = index_memory_usage._append(result)
return result

def transpose(self, *args, copy: bool = False) -> DataFrame:
def transpose(
self,
*args,
copy: bool | lib.NoDefault = lib.no_default,
) -> DataFrame:
"""
Transpose index and columns.

Expand Down Expand Up @@ -3607,6 +3613,8 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0

Returns
-------
DataFrame
Expand Down Expand Up @@ -3687,6 +3695,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
1 object
dtype: object
"""
self._check_copy_deprecation(copy)
nv.validate_transpose(args, {})
# construct the args

Expand Down Expand Up @@ -5062,9 +5071,9 @@ def set_axis(
labels,
*,
axis: Axis = 0,
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
) -> DataFrame:
return super().set_axis(labels, axis=axis)
return super().set_axis(labels, axis=axis, copy=copy)

@doc(
NDFrame.reindex,
Expand Down Expand Up @@ -5313,7 +5322,7 @@ def rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: Literal[True],
level: Level = ...,
errors: IgnoreRaise = ...,
Expand All @@ -5327,7 +5336,7 @@ def rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: Literal[False] = ...,
level: Level = ...,
errors: IgnoreRaise = ...,
Expand All @@ -5341,7 +5350,7 @@ def rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: bool = ...,
level: Level = ...,
errors: IgnoreRaise = ...,
Expand All @@ -5354,7 +5363,7 @@ def rename(
index: Renamer | None = None,
columns: Renamer | None = None,
axis: Axis | None = None,
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
inplace: bool = False,
level: Level | None = None,
errors: IgnoreRaise = "ignore",
Expand Down Expand Up @@ -5384,7 +5393,7 @@ def rename(
axis : {0 or 'index', 1 or 'columns'}, default 0
Axis to target with ``mapper``. Can be either the axis name
('index', 'columns') or number (0, 1). The default is 'index'.
copy : bool, default True
copy : bool, default False
Also copy underlying data.

.. note::
Expand All @@ -5398,6 +5407,8 @@ def rename(

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0
inplace : bool, default False
Whether to modify the DataFrame rather than creating a new one.
If True then value of copy is ignored.
Expand Down Expand Up @@ -5478,6 +5489,7 @@ def rename(
2 2 5
4 3 6
"""
self._check_copy_deprecation(copy)
return super()._rename(
mapper=mapper,
index=index,
Expand Down Expand Up @@ -10657,10 +10669,12 @@ def merge(
right_index: bool = False,
sort: bool = False,
suffixes: Suffixes = ("_x", "_y"),
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
indicator: str | bool = False,
validate: MergeValidate | None = None,
) -> DataFrame:
self._check_copy_deprecation(copy)

from pandas.core.reshape.merge import merge

return merge(
Expand Down Expand Up @@ -12462,7 +12476,7 @@ def to_timestamp(
freq: Frequency | None = None,
how: ToTimestampHow = "start",
axis: Axis = 0,
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
) -> DataFrame:
"""
Cast to DatetimeIndex of timestamps, at *beginning* of period.
Expand All @@ -12476,7 +12490,7 @@ def to_timestamp(
vs. end.
axis : {0 or 'index', 1 or 'columns'}, default 0
The axis to convert (the index by default).
copy : bool, default True
copy : bool, default False
If False then underlying input data is not copied.

.. note::
Expand All @@ -12491,6 +12505,8 @@ def to_timestamp(
You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0

Returns
-------
DataFrame
Expand Down Expand Up @@ -12527,6 +12543,7 @@ def to_timestamp(
>>> df2.index
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
"""
self._check_copy_deprecation(copy)
new_obj = self.copy(deep=False)

axis_name = self._get_axis_name(axis)
Expand All @@ -12540,7 +12557,10 @@ def to_timestamp(
return new_obj

def to_period(
self, freq: Frequency | None = None, axis: Axis = 0, copy: bool | None = None
self,
freq: Frequency | None = None,
axis: Axis = 0,
copy: bool | lib.NoDefault = lib.no_default,
) -> DataFrame:
"""
Convert DataFrame from DatetimeIndex to PeriodIndex.
Expand All @@ -12554,7 +12574,7 @@ def to_period(
Frequency of the PeriodIndex.
axis : {0 or 'index', 1 or 'columns'}, default 0
The axis to convert (the index by default).
copy : bool, default True
copy : bool, default False
If False then underlying input data is not copied.

.. note::
Expand All @@ -12569,6 +12589,8 @@ def to_period(
You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0

Returns
-------
DataFrame
Expand Down Expand Up @@ -12596,6 +12618,7 @@ def to_period(
>>> idx.to_period("Y")
PeriodIndex(['2001', '2002', '2003'], dtype='period[Y-DEC]')
"""
self._check_copy_deprecation(copy)
new_obj = self.copy(deep=False)

axis_name = self._get_axis_name(axis)
Expand Down
31 changes: 18 additions & 13 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def flags(self) -> Flags:
def set_flags(
self,
*,
copy: bool = False,
copy: bool | lib.NoDefault = lib.no_default,
allows_duplicate_labels: bool | None = None,
) -> Self:
"""
Expand All @@ -420,6 +420,8 @@ def set_flags(

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0
allows_duplicate_labels : bool, optional
Whether the returned object allows duplicate labels.

Expand Down Expand Up @@ -454,6 +456,7 @@ def set_flags(
>>> df2.flags.allows_duplicate_labels
False
"""
self._check_copy_deprecation(copy)
df = self.copy(deep=False)
if allows_duplicate_labels is not None:
df.flags["allows_duplicate_labels"] = allows_duplicate_labels
Expand Down Expand Up @@ -679,7 +682,7 @@ def set_axis(
labels,
*,
axis: Axis = 0,
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
) -> Self:
"""
Assign desired index to given axis.
Expand All @@ -696,7 +699,7 @@ def set_axis(
The axis to update. The value 0 identifies the rows. For `Series`
this parameter is unused and defaults to 0.

copy : bool, default True
copy : bool, default False
Whether to make a copy of the underlying data.

.. note::
Expand All @@ -711,6 +714,8 @@ def set_axis(
You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0

Returns
-------
%(klass)s
Expand All @@ -720,6 +725,7 @@ def set_axis(
--------
%(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
"""
self._check_copy_deprecation(copy)
return self._set_axis_nocheck(labels, axis, inplace=False)

@overload
Expand Down Expand Up @@ -948,7 +954,6 @@ def _rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
inplace: Literal[False] = ...,
level: Level | None = ...,
errors: str = ...,
Expand All @@ -962,7 +967,6 @@ def _rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
inplace: Literal[True],
level: Level | None = ...,
errors: str = ...,
Expand All @@ -976,7 +980,6 @@ def _rename(
index: Renamer | None = ...,
columns: Renamer | None = ...,
axis: Axis | None = ...,
copy: bool | None = ...,
inplace: bool,
level: Level | None = ...,
errors: str = ...,
Expand All @@ -990,7 +993,6 @@ def _rename(
index: Renamer | None = None,
columns: Renamer | None = None,
axis: Axis | None = None,
copy: bool | None = None,
inplace: bool = False,
level: Level | None = None,
errors: str = "ignore",
Expand Down Expand Up @@ -1061,7 +1063,7 @@ def rename_axis(
index=...,
columns=...,
axis: Axis = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: Literal[False] = ...,
) -> Self: ...

Expand All @@ -1073,7 +1075,7 @@ def rename_axis(
index=...,
columns=...,
axis: Axis = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: Literal[True],
) -> None: ...

Expand All @@ -1085,7 +1087,7 @@ def rename_axis(
index=...,
columns=...,
axis: Axis = ...,
copy: bool | None = ...,
copy: bool | lib.NoDefault = lib.no_default,
inplace: bool = ...,
) -> Self | None: ...

Expand All @@ -1096,7 +1098,7 @@ def rename_axis(
index=lib.no_default,
columns=lib.no_default,
axis: Axis = 0,
copy: bool | None = None,
copy: bool | lib.NoDefault = lib.no_default,
inplace: bool = False,
) -> Self | None:
"""
Expand All @@ -1118,7 +1120,7 @@ def rename_axis(
apply to that axis' values.
axis : {0 or 'index', 1 or 'columns'}, default 0
The axis to rename.
copy : bool, default None
copy : bool, default False
Also copy underlying data.

.. note::
Expand All @@ -1132,6 +1134,8 @@ def rename_axis(

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

.. deprecated:: 3.0.0
inplace : bool, default False
Modifies the object directly, instead of creating a new Series
or DataFrame.
Expand Down Expand Up @@ -1219,6 +1223,7 @@ class name
cat 4 0
monkey 2 2
"""
self._check_copy_deprecation(copy)
axes = {"index": index, "columns": columns}

if axis is not None:
Expand Down Expand Up @@ -6327,7 +6332,7 @@ def astype(
return self.copy(deep=False)

# GH 19920: retain column metadata after concat
result = concat(results, axis=1, copy=False)
result = concat(results, axis=1)
# GH#40810 retain subclass
# error: Incompatible types in assignment
# (expression has type "Self", variable has type "DataFrame")
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/interchange/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, df: DataFrame, allow_copy: bool = True) -> None:
Constructor - an instance of this (private) class is returned from
`pd.DataFrame.__dataframe__`.
"""
self._df = df.rename(columns=str, copy=False)
self._df = df.rename(columns=str)
self._allow_copy = allow_copy
for i, _col in enumerate(self._df.columns):
rechunked = maybe_rechunk(self._df.iloc[:, i], allow_copy=allow_copy)
Expand Down
Loading