Skip to content

Commit b3d1805

Browse files
authored
CLN: Misc copy-on-write cleanups (#58741)
* CLN: Remove using_cow * Remove unused contexts * Remove some COW comments in arrow * Remove other copy
1 parent 4fe9fac commit b3d1805

File tree

7 files changed

+21
-42
lines changed

7 files changed

+21
-42
lines changed

pandas/_config/__init__.py

-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"describe_option",
1616
"option_context",
1717
"options",
18-
"using_copy_on_write",
1918
]
2019
from pandas._config import config
2120
from pandas._config import dates # pyright: ignore[reportUnusedImport] # noqa: F401
@@ -31,10 +30,6 @@
3130
from pandas._config.display import detect_console_encoding
3231

3332

34-
def using_copy_on_write() -> bool:
35-
return True
36-
37-
3833
def using_pyarrow_string_dtype() -> bool:
3934
_mode_options = _global_config["future"]
4035
return _mode_options["infer_string"]

pandas/_testing/contexts.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
)
1212
import uuid
1313

14-
from pandas._config import using_copy_on_write
15-
1614
from pandas.compat import PYPY
1715
from pandas.errors import ChainedAssignmentError
1816

@@ -158,34 +156,25 @@ def with_csv_dialect(name: str, **kwargs) -> Generator[None, None, None]:
158156
csv.unregister_dialect(name)
159157

160158

161-
def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()):
159+
def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
162160
from pandas._testing import assert_produces_warning
163161

164-
if not warn:
165-
from contextlib import nullcontext
166-
167-
return nullcontext()
168-
169-
if PYPY and not extra_warnings:
170-
from contextlib import nullcontext
162+
if PYPY:
163+
if not extra_warnings:
164+
from contextlib import nullcontext
171165

172-
return nullcontext()
173-
elif PYPY and extra_warnings:
174-
return assert_produces_warning(
175-
extra_warnings,
176-
match=extra_match,
177-
)
178-
else:
179-
if using_copy_on_write():
180-
warning = ChainedAssignmentError
181-
match = (
182-
"A value is trying to be set on a copy of a DataFrame or Series "
183-
"through chained assignment"
184-
)
166+
return nullcontext()
185167
else:
186-
warning = FutureWarning # type: ignore[assignment]
187-
# TODO update match
188-
match = "ChainedAssignmentError"
168+
return assert_produces_warning(
169+
extra_warnings,
170+
match=extra_match,
171+
)
172+
else:
173+
warning = ChainedAssignmentError
174+
match = (
175+
"A value is trying to be set on a copy of a DataFrame or Series "
176+
"through chained assignment"
177+
)
189178
if extra_warnings:
190179
warning = (warning, *extra_warnings) # type: ignore[assignment]
191180
return assert_produces_warning(

pandas/core/arrays/arrow/array.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,7 @@ def _pad_or_backfill(
10521052
copy: bool = True,
10531053
) -> Self:
10541054
if not self._hasna:
1055-
# TODO(CoW): Not necessary anymore when CoW is the default
1056-
return self.copy()
1055+
return self
10571056

10581057
if limit is None and limit_area is None:
10591058
method = missing.clean_fill_method(method)
@@ -1084,7 +1083,6 @@ def fillna(
10841083
copy: bool = True,
10851084
) -> Self:
10861085
if not self._hasna:
1087-
# TODO(CoW): Not necessary anymore when CoW is the default
10881086
return self.copy()
10891087

10901088
if limit is not None:

pandas/core/indexing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2372,8 +2372,7 @@ def ravel(i):
23722372
# we have a frame, with multiple indexers on both axes; and a
23732373
# series, so need to broadcast (see GH5206)
23742374
if sum_aligners == self.ndim and all(is_sequence(_) for _ in indexer):
2375-
# TODO(CoW): copy shouldn't be needed here
2376-
ser_values = ser.reindex(obj.axes[0][indexer[0]]).copy()._values
2375+
ser_values = ser.reindex(obj.axes[0][indexer[0]])._values
23772376

23782377
# single indexer
23792378
if len(indexer) > 1 and not multiindex_indexer:

pandas/tests/copy_view/test_astype.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from pandas.compat.pyarrow import pa_version_under12p0
77
import pandas.util._test_decorators as td
88

9-
import pandas as pd
109
from pandas import (
1110
DataFrame,
1211
Series,
@@ -79,8 +78,7 @@ def test_astype_different_target_dtype(dtype):
7978

8079
def test_astype_numpy_to_ea():
8180
ser = Series([1, 2, 3])
82-
with pd.option_context("mode.copy_on_write", True):
83-
result = ser.astype("Int64")
81+
result = ser.astype("Int64")
8482
assert np.shares_memory(get_array(ser), get_array(result))
8583

8684

pandas/tests/indexes/base_class/test_constructors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,5 @@ def test_inference_on_pandas_objects(self):
7575
def test_constructor_not_read_only(self):
7676
# GH#57130
7777
ser = Series([1, 2], dtype=object)
78-
with pd.option_context("mode.copy_on_write", True):
79-
idx = Index(ser)
80-
assert idx._values.flags.writeable
78+
idx = Index(ser)
79+
assert idx._values.flags.writeable

pandas/tests/indexing/test_chaining_and_caching.py

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def test_detect_chained_assignment_changing_dtype(self):
284284
with tm.raises_chained_assignment_error():
285285
df.loc[2]["C"] = "foo"
286286
tm.assert_frame_equal(df, df_original)
287+
# TODO: Use tm.raises_chained_assignment_error() when PDEP-6 is enforced
287288
with tm.raises_chained_assignment_error(
288289
extra_warnings=(FutureWarning,), extra_match=(None,)
289290
):

0 commit comments

Comments
 (0)