Skip to content

Commit

Permalink
CLN: Misc copy-on-write cleanups (#58741)
Browse files Browse the repository at this point in the history
* CLN: Remove using_cow

* Remove unused contexts

* Remove some COW comments in arrow

* Remove other copy
  • Loading branch information
mroeschke committed May 16, 2024
1 parent 4fe9fac commit b3d1805
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 42 deletions.
5 changes: 0 additions & 5 deletions pandas/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"describe_option",
"option_context",
"options",
"using_copy_on_write",
]
from pandas._config import config
from pandas._config import dates # pyright: ignore[reportUnusedImport] # noqa: F401
Expand All @@ -31,10 +30,6 @@
from pandas._config.display import detect_console_encoding


def using_copy_on_write() -> bool:
return True


def using_pyarrow_string_dtype() -> bool:
_mode_options = _global_config["future"]
return _mode_options["infer_string"]
41 changes: 15 additions & 26 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
)
import uuid

from pandas._config import using_copy_on_write

from pandas.compat import PYPY
from pandas.errors import ChainedAssignmentError

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


def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()):
def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
from pandas._testing import assert_produces_warning

if not warn:
from contextlib import nullcontext

return nullcontext()

if PYPY and not extra_warnings:
from contextlib import nullcontext
if PYPY:
if not extra_warnings:
from contextlib import nullcontext

return nullcontext()
elif PYPY and extra_warnings:
return assert_produces_warning(
extra_warnings,
match=extra_match,
)
else:
if using_copy_on_write():
warning = ChainedAssignmentError
match = (
"A value is trying to be set on a copy of a DataFrame or Series "
"through chained assignment"
)
return nullcontext()
else:
warning = FutureWarning # type: ignore[assignment]
# TODO update match
match = "ChainedAssignmentError"
return assert_produces_warning(
extra_warnings,
match=extra_match,
)
else:
warning = ChainedAssignmentError
match = (
"A value is trying to be set on a copy of a DataFrame or Series "
"through chained assignment"
)
if extra_warnings:
warning = (warning, *extra_warnings) # type: ignore[assignment]
return assert_produces_warning(
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,7 @@ def _pad_or_backfill(
copy: bool = True,
) -> Self:
if not self._hasna:
# TODO(CoW): Not necessary anymore when CoW is the default
return self.copy()
return self

if limit is None and limit_area is None:
method = missing.clean_fill_method(method)
Expand Down Expand Up @@ -1084,7 +1083,6 @@ def fillna(
copy: bool = True,
) -> Self:
if not self._hasna:
# TODO(CoW): Not necessary anymore when CoW is the default
return self.copy()

if limit is not None:
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2372,8 +2372,7 @@ def ravel(i):
# we have a frame, with multiple indexers on both axes; and a
# series, so need to broadcast (see GH5206)
if sum_aligners == self.ndim and all(is_sequence(_) for _ in indexer):
# TODO(CoW): copy shouldn't be needed here
ser_values = ser.reindex(obj.axes[0][indexer[0]]).copy()._values
ser_values = ser.reindex(obj.axes[0][indexer[0]])._values

# single indexer
if len(indexer) > 1 and not multiindex_indexer:
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/copy_view/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pandas.compat.pyarrow import pa_version_under12p0
import pandas.util._test_decorators as td

import pandas as pd
from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -79,8 +78,7 @@ def test_astype_different_target_dtype(dtype):

def test_astype_numpy_to_ea():
ser = Series([1, 2, 3])
with pd.option_context("mode.copy_on_write", True):
result = ser.astype("Int64")
result = ser.astype("Int64")
assert np.shares_memory(get_array(ser), get_array(result))


Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/indexes/base_class/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ def test_inference_on_pandas_objects(self):
def test_constructor_not_read_only(self):
# GH#57130
ser = Series([1, 2], dtype=object)
with pd.option_context("mode.copy_on_write", True):
idx = Index(ser)
assert idx._values.flags.writeable
idx = Index(ser)
assert idx._values.flags.writeable
1 change: 1 addition & 0 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def test_detect_chained_assignment_changing_dtype(self):
with tm.raises_chained_assignment_error():
df.loc[2]["C"] = "foo"
tm.assert_frame_equal(df, df_original)
# TODO: Use tm.raises_chained_assignment_error() when PDEP-6 is enforced
with tm.raises_chained_assignment_error(
extra_warnings=(FutureWarning,), extra_match=(None,)
):
Expand Down

0 comments on commit b3d1805

Please sign in to comment.