Skip to content

Commit 0c06dd3

Browse files
phoflpmhatre1
authored andcommitted
Remove Copy-on-Write warning mode (pandas-dev#57234)
1 parent b7da9c5 commit 0c06dd3

File tree

9 files changed

+35
-428
lines changed

9 files changed

+35
-428
lines changed

pandas/_config/__init__.py

-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"option_context",
1616
"options",
1717
"using_copy_on_write",
18-
"warn_copy_on_write",
1918
]
2019
from pandas._config import config
2120
from pandas._config import dates # pyright: ignore[reportUnusedImport] # noqa: F401
@@ -35,10 +34,6 @@ def using_copy_on_write() -> bool:
3534
return True
3635

3736

38-
def warn_copy_on_write() -> bool:
39-
return False
40-
41-
4237
def using_nullable_dtypes() -> bool:
4338
_mode_options = _global_config["mode"]
4439
return _mode_options["nullable_dtypes"]

pandas/core/frame.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from pandas._config import (
4343
get_option,
4444
using_copy_on_write,
45-
warn_copy_on_write,
4645
)
4746

4847
from pandas._libs import (
@@ -64,7 +63,6 @@
6463
_chained_assignment_method_msg,
6564
_chained_assignment_msg,
6665
_chained_assignment_warning_method_msg,
67-
_chained_assignment_warning_msg,
6866
)
6967
from pandas.util._decorators import (
7068
Appender,
@@ -4199,17 +4197,6 @@ def __setitem__(self, key, value) -> None:
41994197
warnings.warn(
42004198
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
42014199
)
4202-
elif not PYPY and not using_copy_on_write():
4203-
if sys.getrefcount(self) <= 3 and (
4204-
warn_copy_on_write()
4205-
or (
4206-
not warn_copy_on_write()
4207-
and any(b.refs.has_reference() for b in self._mgr.blocks)
4208-
)
4209-
):
4210-
warnings.warn(
4211-
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
4212-
)
42134200

42144201
key = com.apply_if_callable(key, self)
42154202

@@ -4550,7 +4537,7 @@ def _clear_item_cache(self) -> None:
45504537

45514538
def _get_item_cache(self, item: Hashable) -> Series:
45524539
"""Return the cached item, item represents a label indexer."""
4553-
if using_copy_on_write() or warn_copy_on_write():
4540+
if using_copy_on_write():
45544541
loc = self.columns.get_loc(item)
45554542
return self._ixs(loc, axis=1)
45564543

pandas/core/generic.py

+2-110
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from pandas._config import (
3131
config,
3232
using_copy_on_write,
33-
warn_copy_on_write,
3433
)
3534

3635
from pandas._libs import lib
@@ -105,7 +104,6 @@
105104
from pandas.errors.cow import (
106105
_chained_assignment_method_msg,
107106
_chained_assignment_warning_method_msg,
108-
_check_cacher,
109107
)
110108
from pandas.util._decorators import (
111109
deprecate_nonkeyword_arguments,
@@ -4407,7 +4405,7 @@ def _check_setitem_copy(self, t: str = "setting", force: bool_t = False) -> None
44074405
df.iloc[0:5]['group'] = 'a'
44084406
44094407
"""
4410-
if using_copy_on_write() or warn_copy_on_write():
4408+
if using_copy_on_write():
44114409
return
44124410

44134411
# return early if the check is not needed
@@ -7256,22 +7254,6 @@ def fillna(
72567254
ChainedAssignmentError,
72577255
stacklevel=2,
72587256
)
7259-
elif (
7260-
not PYPY
7261-
and not using_copy_on_write()
7262-
and self._is_view_after_cow_rules()
7263-
):
7264-
ctr = sys.getrefcount(self)
7265-
ref_count = REF_COUNT
7266-
if isinstance(self, ABCSeries) and _check_cacher(self):
7267-
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7268-
ref_count += 1
7269-
if ctr <= ref_count:
7270-
warnings.warn(
7271-
_chained_assignment_warning_method_msg,
7272-
FutureWarning,
7273-
stacklevel=2,
7274-
)
72757257

72767258
value, method = validate_fillna_kwargs(value, method)
72777259
if method is not None:
@@ -7559,22 +7541,6 @@ def ffill(
75597541
ChainedAssignmentError,
75607542
stacklevel=2,
75617543
)
7562-
elif (
7563-
not PYPY
7564-
and not using_copy_on_write()
7565-
and self._is_view_after_cow_rules()
7566-
):
7567-
ctr = sys.getrefcount(self)
7568-
ref_count = REF_COUNT
7569-
if isinstance(self, ABCSeries) and _check_cacher(self):
7570-
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7571-
ref_count += 1
7572-
if ctr <= ref_count:
7573-
warnings.warn(
7574-
_chained_assignment_warning_method_msg,
7575-
FutureWarning,
7576-
stacklevel=2,
7577-
)
75787544

75797545
return self._pad_or_backfill(
75807546
"ffill",
@@ -7763,22 +7729,6 @@ def bfill(
77637729
ChainedAssignmentError,
77647730
stacklevel=2,
77657731
)
7766-
elif (
7767-
not PYPY
7768-
and not using_copy_on_write()
7769-
and self._is_view_after_cow_rules()
7770-
):
7771-
ctr = sys.getrefcount(self)
7772-
ref_count = REF_COUNT
7773-
if isinstance(self, ABCSeries) and _check_cacher(self):
7774-
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
7775-
ref_count += 1
7776-
if ctr <= ref_count:
7777-
warnings.warn(
7778-
_chained_assignment_warning_method_msg,
7779-
FutureWarning,
7780-
stacklevel=2,
7781-
)
77827732

77837733
return self._pad_or_backfill(
77847734
"bfill",
@@ -7934,26 +7884,6 @@ def replace(
79347884
ChainedAssignmentError,
79357885
stacklevel=2,
79367886
)
7937-
elif (
7938-
not PYPY
7939-
and not using_copy_on_write()
7940-
and self._is_view_after_cow_rules()
7941-
):
7942-
ctr = sys.getrefcount(self)
7943-
ref_count = REF_COUNT
7944-
if isinstance(self, ABCSeries) and _check_cacher(self):
7945-
# in non-CoW mode, chained Series access will populate the
7946-
# `_item_cache` which results in an increased ref count not below
7947-
# the threshold, while we still need to warn. We detect this case
7948-
# of a Series derived from a DataFrame through the presence of
7949-
# checking the `_cacher`
7950-
ref_count += 1
7951-
if ctr <= ref_count:
7952-
warnings.warn(
7953-
_chained_assignment_warning_method_msg,
7954-
FutureWarning,
7955-
stacklevel=2,
7956-
)
79577887

79587888
if not is_bool(regex) and to_replace is not None:
79597889
raise ValueError("'to_replace' must be 'None' if 'regex' is not a bool")
@@ -8384,22 +8314,6 @@ def interpolate(
83848314
ChainedAssignmentError,
83858315
stacklevel=2,
83868316
)
8387-
elif (
8388-
not PYPY
8389-
and not using_copy_on_write()
8390-
and self._is_view_after_cow_rules()
8391-
):
8392-
ctr = sys.getrefcount(self)
8393-
ref_count = REF_COUNT
8394-
if isinstance(self, ABCSeries) and _check_cacher(self):
8395-
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
8396-
ref_count += 1
8397-
if ctr <= ref_count:
8398-
warnings.warn(
8399-
_chained_assignment_warning_method_msg,
8400-
FutureWarning,
8401-
stacklevel=2,
8402-
)
84038317

84048318
axis = self._get_axis_number(axis)
84058319

@@ -10569,7 +10483,6 @@ def _where(
1056910483
inplace: bool_t = False,
1057010484
axis: Axis | None = None,
1057110485
level=None,
10572-
warn: bool_t = True,
1057310486
):
1057410487
"""
1057510488
Equivalent to public method `where`, except that `other` is not
@@ -10700,7 +10613,7 @@ def _where(
1070010613
# we may have different type blocks come out of putmask, so
1070110614
# reconstruct the block manager
1070210615

10703-
new_data = self._mgr.putmask(mask=cond, new=other, align=align, warn=warn)
10616+
new_data = self._mgr.putmask(mask=cond, new=other, align=align)
1070410617
result = self._constructor_from_mgr(new_data, axes=new_data.axes)
1070510618
return self._update_inplace(result)
1070610619

@@ -12566,29 +12479,8 @@ def _inplace_method(self, other, op) -> Self:
1256612479
"""
1256712480
Wrap arithmetic method to operate inplace.
1256812481
"""
12569-
warn = True
12570-
if not PYPY and warn_copy_on_write():
12571-
if sys.getrefcount(self) <= REF_COUNT + 2:
12572-
# we are probably in an inplace setitem context (e.g. df['a'] += 1)
12573-
warn = False
12574-
1257512482
result = op(self, other)
1257612483

12577-
if (
12578-
self.ndim == 1
12579-
and result._indexed_same(self)
12580-
and result.dtype == self.dtype
12581-
and not using_copy_on_write()
12582-
and not (warn_copy_on_write() and not warn)
12583-
):
12584-
# GH#36498 this inplace op can _actually_ be inplace.
12585-
# Item "BlockManager" of "Union[BlockManager, SingleBlockManager]" has
12586-
# no attribute "setitem_inplace"
12587-
self._mgr.setitem_inplace( # type: ignore[union-attr]
12588-
slice(None), result._values, warn=warn
12589-
)
12590-
return self
12591-
1259212484
# Delete cacher
1259312485
self._reset_cacher()
1259412486

pandas/core/groupby/grouper.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212

1313
import numpy as np
1414

15-
from pandas._config import (
16-
using_copy_on_write,
17-
warn_copy_on_write,
18-
)
15+
from pandas._config import using_copy_on_write
1916

2017
from pandas._libs.tslibs import OutOfBoundsDatetime
2118
from pandas.errors import InvalidIndexError
@@ -962,7 +959,7 @@ def is_in_axis(key) -> bool:
962959
def is_in_obj(gpr) -> bool:
963960
if not hasattr(gpr, "name"):
964961
return False
965-
if using_copy_on_write() or warn_copy_on_write():
962+
if using_copy_on_write():
966963
# For the CoW case, we check the references to determine if the
967964
# series is part of the object
968965
try:

pandas/core/indexing.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
import numpy as np
1515

16-
from pandas._config import (
17-
using_copy_on_write,
18-
warn_copy_on_write,
19-
)
16+
from pandas._config import using_copy_on_write
2017

2118
from pandas._libs.indexing import NDFrameIndexerBase
2219
from pandas._libs.lib import item_from_zerodim
@@ -28,11 +25,7 @@
2825
InvalidIndexError,
2926
LossySetitemError,
3027
)
31-
from pandas.errors.cow import (
32-
_chained_assignment_msg,
33-
_chained_assignment_warning_msg,
34-
_check_cacher,
35-
)
28+
from pandas.errors.cow import _chained_assignment_msg
3629
from pandas.util._decorators import doc
3730
from pandas.util._exceptions import find_stack_level
3831

@@ -889,16 +882,6 @@ def __setitem__(self, key, value) -> None:
889882
warnings.warn(
890883
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
891884
)
892-
elif not PYPY and not using_copy_on_write():
893-
ctr = sys.getrefcount(self.obj)
894-
ref_count = 2
895-
if not warn_copy_on_write() and _check_cacher(self.obj):
896-
# see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
897-
ref_count += 1
898-
if ctr <= ref_count:
899-
warnings.warn(
900-
_chained_assignment_warning_msg, FutureWarning, stacklevel=2
901-
)
902885

903886
check_dict_or_set_indexers(key)
904887
if isinstance(key, tuple):

0 commit comments

Comments
 (0)