Skip to content

Commit 7ff60c6

Browse files
authored
CoW: Enforce some deprecations on the datafame level (#57254)
* CoW: Enforce some deprecations on the datafame level * Remove copy keyword * Fixup
1 parent 72a07f6 commit 7ff60c6

File tree

4 files changed

+75
-236
lines changed

4 files changed

+75
-236
lines changed

pandas/core/frame.py

+15-42
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
import numpy as np
4040
from numpy import ma
4141

42-
from pandas._config import (
43-
get_option,
44-
using_copy_on_write,
45-
)
42+
from pandas._config import get_option
4643

4744
from pandas._libs import (
4845
algos as libalgos,
@@ -62,7 +59,6 @@
6259
from pandas.errors.cow import (
6360
_chained_assignment_method_msg,
6461
_chained_assignment_msg,
65-
_chained_assignment_warning_method_msg,
6662
)
6763
from pandas.util._decorators import (
6864
Appender,
@@ -707,8 +703,7 @@ def __init__(
707703
stacklevel=1, # bump to 2 once pyarrow 15.0 is released with fix
708704
)
709705

710-
if using_copy_on_write():
711-
data = data.copy(deep=False)
706+
data = data.copy(deep=False)
712707
# first check if a Manager is passed without any other arguments
713708
# -> use fastpath (without checking Manager type)
714709
if index is None and columns is None and dtype is None and not copy:
@@ -730,9 +725,7 @@ def __init__(
730725
if isinstance(data, dict):
731726
# retain pre-GH#38939 default behavior
732727
copy = True
733-
elif using_copy_on_write() and not isinstance(
734-
data, (Index, DataFrame, Series)
735-
):
728+
elif not isinstance(data, (Index, DataFrame, Series)):
736729
copy = True
737730
else:
738731
copy = False
@@ -785,15 +778,14 @@ def __init__(
785778
)
786779
elif getattr(data, "name", None) is not None:
787780
# i.e. Series/Index with non-None name
788-
_copy = copy if using_copy_on_write() else True
789781
mgr = dict_to_mgr(
790782
# error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
791783
# attribute "name"
792784
{data.name: data}, # type: ignore[union-attr]
793785
index,
794786
columns,
795787
dtype=dtype,
796-
copy=_copy,
788+
copy=copy,
797789
)
798790
else:
799791
mgr = ndarray_to_mgr(
@@ -1500,10 +1492,9 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
15001492
"""
15011493
columns = self.columns
15021494
klass = self._constructor_sliced
1503-
using_cow = using_copy_on_write()
15041495
for k, v in zip(self.index, self.values):
15051496
s = klass(v, index=columns, name=k).__finalize__(self)
1506-
if using_cow and self._mgr.is_single_block:
1497+
if self._mgr.is_single_block:
15071498
s._mgr.add_references(self._mgr)
15081499
yield k, s
15091500

@@ -3669,8 +3660,6 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
36693660
if self._can_fast_transpose:
36703661
# Note: tests pass without this, but this improves perf quite a bit.
36713662
new_vals = self._values.T
3672-
if copy and not using_copy_on_write():
3673-
new_vals = new_vals.copy()
36743663

36753664
result = self._constructor(
36763665
new_vals,
@@ -3679,7 +3668,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
36793668
copy=False,
36803669
dtype=new_vals.dtype,
36813670
)
3682-
if using_copy_on_write() and len(self) > 0:
3671+
if len(self) > 0:
36833672
result._mgr.add_references(self._mgr)
36843673

36853674
elif (
@@ -3723,8 +3712,6 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
37233712

37243713
else:
37253714
new_arr = self.values.T
3726-
if copy and not using_copy_on_write():
3727-
new_arr = new_arr.copy()
37283715
result = self._constructor(
37293716
new_arr,
37303717
index=self.columns,
@@ -4043,7 +4030,7 @@ def isetitem(self, loc, value) -> None:
40434030
self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs)
40444031

40454032
def __setitem__(self, key, value) -> None:
4046-
if not PYPY and using_copy_on_write():
4033+
if not PYPY:
40474034
if sys.getrefcount(self) <= 3:
40484035
warnings.warn(
40494036
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
@@ -4251,12 +4238,7 @@ def _set_item_mgr(
42514238
def _iset_item(self, loc: int, value: Series, inplace: bool = True) -> None:
42524239
# We are only called from _replace_columnwise which guarantees that
42534240
# no reindex is necessary
4254-
if using_copy_on_write():
4255-
self._iset_item_mgr(
4256-
loc, value._values, inplace=inplace, refs=value._references
4257-
)
4258-
else:
4259-
self._iset_item_mgr(loc, value._values.copy(), inplace=True)
4241+
self._iset_item_mgr(loc, value._values, inplace=inplace, refs=value._references)
42604242

42614243
def _set_item(self, key, value) -> None:
42624244
"""
@@ -4992,9 +4974,7 @@ def _series(self):
49924974
# ----------------------------------------------------------------------
49934975
# Reindexing and alignment
49944976

4995-
def _reindex_multi(
4996-
self, axes: dict[str, Index], copy: bool, fill_value
4997-
) -> DataFrame:
4977+
def _reindex_multi(self, axes: dict[str, Index], fill_value) -> DataFrame:
49984978
"""
49994979
We are guaranteed non-Nones in the axes.
50004980
"""
@@ -5016,7 +4996,7 @@ def _reindex_multi(
50164996
else:
50174997
return self._reindex_with_indexers(
50184998
{0: [new_index, row_indexer], 1: [new_columns, col_indexer]},
5019-
copy=copy,
4999+
copy=False,
50205000
fill_value=fill_value,
50215001
)
50225002

@@ -6937,7 +6917,7 @@ def sort_values(
69376917
return self.copy(deep=None)
69386918

69396919
if is_range_indexer(indexer, len(indexer)):
6940-
result = self.copy(deep=(not inplace and not using_copy_on_write()))
6920+
result = self.copy(deep=False)
69416921
if ignore_index:
69426922
result.index = default_index(len(result))
69436923

@@ -8745,20 +8725,13 @@ def update(
87458725
1 2 500.0
87468726
2 3 6.0
87478727
"""
8748-
if not PYPY and using_copy_on_write():
8728+
if not PYPY:
87498729
if sys.getrefcount(self) <= REF_COUNT:
87508730
warnings.warn(
87518731
_chained_assignment_method_msg,
87528732
ChainedAssignmentError,
87538733
stacklevel=2,
87548734
)
8755-
elif not PYPY and not using_copy_on_write() and self._is_view_after_cow_rules():
8756-
if sys.getrefcount(self) <= REF_COUNT:
8757-
warnings.warn(
8758-
_chained_assignment_warning_method_msg,
8759-
FutureWarning,
8760-
stacklevel=2,
8761-
)
87628735

87638736
# TODO: Support other joins
87648737
if join != "left": # pragma: no cover
@@ -12053,7 +12026,7 @@ def to_timestamp(
1205312026
>>> df2.index
1205412027
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
1205512028
"""
12056-
new_obj = self.copy(deep=copy and not using_copy_on_write())
12029+
new_obj = self.copy(deep=False)
1205712030

1205812031
axis_name = self._get_axis_name(axis)
1205912032
old_ax = getattr(self, axis_name)
@@ -12122,7 +12095,7 @@ def to_period(
1212212095
>>> idx.to_period("Y")
1212312096
PeriodIndex(['2001', '2002', '2003'], dtype='period[Y-DEC]')
1212412097
"""
12125-
new_obj = self.copy(deep=copy and not using_copy_on_write())
12098+
new_obj = self.copy(deep=False)
1212612099

1212712100
axis_name = self._get_axis_name(axis)
1212812101
old_ax = getattr(self, axis_name)
@@ -12445,7 +12418,7 @@ def _reindex_for_setitem(
1244512418
# reindex if necessary
1244612419

1244712420
if value.index.equals(index) or not len(index):
12448-
if using_copy_on_write() and isinstance(value, Series):
12421+
if isinstance(value, Series):
1244912422
return value._values, value._references
1245012423
return value._values.copy(), None
1245112424

0 commit comments

Comments
 (0)