Skip to content

CoW: Enforce some deprecations on the datafame level #57254

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

Merged
merged 5 commits into from
Feb 9, 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
57 changes: 15 additions & 42 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
import numpy as np
from numpy import ma

from pandas._config import (
get_option,
using_copy_on_write,
)
from pandas._config import get_option

from pandas._libs import (
algos as libalgos,
Expand All @@ -62,7 +59,6 @@
from pandas.errors.cow import (
_chained_assignment_method_msg,
_chained_assignment_msg,
_chained_assignment_warning_method_msg,
)
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -707,8 +703,7 @@ def __init__(
stacklevel=1, # bump to 2 once pyarrow 15.0 is released with fix
)

if using_copy_on_write():
data = data.copy(deep=False)
data = data.copy(deep=False)
# first check if a Manager is passed without any other arguments
# -> use fastpath (without checking Manager type)
if index is None and columns is None and dtype is None and not copy:
Expand All @@ -730,9 +725,7 @@ def __init__(
if isinstance(data, dict):
# retain pre-GH#38939 default behavior
copy = True
elif using_copy_on_write() and not isinstance(
data, (Index, DataFrame, Series)
):
elif not isinstance(data, (Index, DataFrame, Series)):
copy = True
else:
copy = False
Expand Down Expand Up @@ -785,15 +778,14 @@ def __init__(
)
elif getattr(data, "name", None) is not None:
# i.e. Series/Index with non-None name
_copy = copy if using_copy_on_write() else True
mgr = dict_to_mgr(
# error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
# attribute "name"
{data.name: data}, # type: ignore[union-attr]
index,
columns,
dtype=dtype,
copy=_copy,
copy=copy,
)
else:
mgr = ndarray_to_mgr(
Expand Down Expand Up @@ -1500,10 +1492,9 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
"""
columns = self.columns
klass = self._constructor_sliced
using_cow = using_copy_on_write()
for k, v in zip(self.index, self.values):
s = klass(v, index=columns, name=k).__finalize__(self)
if using_cow and self._mgr.is_single_block:
if self._mgr.is_single_block:
s._mgr.add_references(self._mgr)
yield k, s

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

result = self._constructor(
new_vals,
Expand All @@ -3679,7 +3668,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
copy=False,
dtype=new_vals.dtype,
)
if using_copy_on_write() and len(self) > 0:
if len(self) > 0:
result._mgr.add_references(self._mgr)

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

else:
new_arr = self.values.T
if copy and not using_copy_on_write():
new_arr = new_arr.copy()
result = self._constructor(
new_arr,
index=self.columns,
Expand Down Expand Up @@ -4043,7 +4030,7 @@ def isetitem(self, loc, value) -> None:
self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs)

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY:
if sys.getrefcount(self) <= 3:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
Expand Down Expand Up @@ -4251,12 +4238,7 @@ def _set_item_mgr(
def _iset_item(self, loc: int, value: Series, inplace: bool = True) -> None:
# We are only called from _replace_columnwise which guarantees that
# no reindex is necessary
if using_copy_on_write():
self._iset_item_mgr(
loc, value._values, inplace=inplace, refs=value._references
)
else:
self._iset_item_mgr(loc, value._values.copy(), inplace=True)
self._iset_item_mgr(loc, value._values, inplace=inplace, refs=value._references)

def _set_item(self, key, value) -> None:
"""
Expand Down Expand Up @@ -4992,9 +4974,7 @@ def _series(self):
# ----------------------------------------------------------------------
# Reindexing and alignment

def _reindex_multi(
self, axes: dict[str, Index], copy: bool, fill_value
) -> DataFrame:
def _reindex_multi(self, axes: dict[str, Index], fill_value) -> DataFrame:
"""
We are guaranteed non-Nones in the axes.
"""
Expand All @@ -5016,7 +4996,7 @@ def _reindex_multi(
else:
return self._reindex_with_indexers(
{0: [new_index, row_indexer], 1: [new_columns, col_indexer]},
copy=copy,
copy=False,
fill_value=fill_value,
)

Expand Down Expand Up @@ -6937,7 +6917,7 @@ def sort_values(
return self.copy(deep=None)

if is_range_indexer(indexer, len(indexer)):
result = self.copy(deep=(not inplace and not using_copy_on_write()))
result = self.copy(deep=False)
if ignore_index:
result.index = default_index(len(result))

Expand Down Expand Up @@ -8745,20 +8725,13 @@ def update(
1 2 500.0
2 3 6.0
"""
if not PYPY and using_copy_on_write():
if not PYPY:
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
stacklevel=2,
)
elif not PYPY and not using_copy_on_write() and self._is_view_after_cow_rules():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_warning_method_msg,
FutureWarning,
stacklevel=2,
)

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

axis_name = self._get_axis_name(axis)
old_ax = getattr(self, axis_name)
Expand Down Expand Up @@ -12122,7 +12095,7 @@ def to_period(
>>> idx.to_period("Y")
PeriodIndex(['2001', '2002', '2003'], dtype='period[Y-DEC]')
"""
new_obj = self.copy(deep=copy and not using_copy_on_write())
new_obj = self.copy(deep=False)

axis_name = self._get_axis_name(axis)
old_ax = getattr(self, axis_name)
Expand Down Expand Up @@ -12445,7 +12418,7 @@ def _reindex_for_setitem(
# reindex if necessary

if value.index.equals(index) or not len(index):
if using_copy_on_write() and isinstance(value, Series):
if isinstance(value, Series):
return value._values, value._references
return value._values.copy(), None

Expand Down
Loading