Skip to content

CoW: Remove remaining occurrences from CoW #57324

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 4 commits into from
Feb 12, 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
6 changes: 2 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import numpy as np

from pandas._config import using_copy_on_write

from pandas._libs import lib
from pandas._typing import (
AxisInt,
Expand Down Expand Up @@ -661,10 +659,10 @@ def to_numpy(

result = np.asarray(values, dtype=dtype)

if (copy and not fillna) or (not copy and using_copy_on_write()):
if (copy and not fillna) or not copy:
if np.shares_memory(self._values[:2], result[:2]):
# Take slices to improve performance of check
if using_copy_on_write() and not copy:
if not copy:
result = result.view()
result.flags.writeable = False
else:
Expand Down
12 changes: 1 addition & 11 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def get_objs_combined_axis(
intersect: bool = False,
axis: Axis = 0,
sort: bool = True,
copy: bool = False,
) -> Index:
"""
Extract combined index: return intersection or union (depending on the
Expand All @@ -92,15 +91,13 @@ def get_objs_combined_axis(
The axis to extract indexes from.
sort : bool, default True
Whether the result index should come out sorted or not.
copy : bool, default False
If True, return a copy of the combined index.

Returns
-------
Index
"""
obs_idxes = [obj._get_axis(axis) for obj in objs]
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)


def _get_distinct_objs(objs: list[Index]) -> list[Index]:
Expand All @@ -121,7 +118,6 @@ def _get_combined_index(
indexes: list[Index],
intersect: bool = False,
sort: bool = False,
copy: bool = False,
) -> Index:
"""
Return the union or intersection of indexes.
Expand All @@ -135,8 +131,6 @@ def _get_combined_index(
calculate the union.
sort : bool, default False
Whether the result index should come out sorted or not.
copy : bool, default False
If True, return a copy of the combined index.

Returns
-------
Expand All @@ -158,10 +152,6 @@ def _get_combined_index(

if sort:
index = safe_sort_index(index)
# GH 29879
if copy:
index = index.copy()

return index


Expand Down
15 changes: 6 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

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

Expand Down Expand Up @@ -1633,7 +1632,7 @@ def to_frame(

if name is lib.no_default:
name = self._get_level_names()
result = DataFrame({name: self}, copy=not using_copy_on_write())
result = DataFrame({name: self}, copy=False)

if index:
result.index = self
Expand Down Expand Up @@ -4773,13 +4772,11 @@ def values(self) -> ArrayLike:
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]
"""
if using_copy_on_write():
data = self._data
if isinstance(data, np.ndarray):
data = data.view()
data.flags.writeable = False
return data
return self._data
data = self._data
if isinstance(data, np.ndarray):
data = data.view()
data.flags.writeable = False
return data

@cache_readonly
@doc(IndexOpsMixin.array)
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import numpy as np

from pandas._config import using_copy_on_write

from pandas._libs import (
NaT,
Timedelta,
Expand Down Expand Up @@ -454,9 +452,8 @@ def _with_freq(self, freq):
def values(self) -> np.ndarray:
# NB: For Datetime64TZ this is lossy
data = self._data._ndarray
if using_copy_on_write():
data = data.view()
data.flags.writeable = False
data = data.view()
data.flags.writeable = False
return data

@doc(DatetimeIndexOpsMixin.shift)
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import numpy as np

from pandas._config import using_copy_on_write

from pandas._libs.indexing import NDFrameIndexerBase
from pandas._libs.lib import item_from_zerodim
from pandas.compat import PYPY
Expand Down Expand Up @@ -894,7 +892,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:

@final
def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY:
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
Expand Down Expand Up @@ -2107,7 +2105,7 @@ def _setitem_with_indexer_frame_value(
tuple(sub_indexer),
value[item],
multiindex_indexer,
using_cow=using_copy_on_write(),
using_cow=True,
)
else:
val = np.nan
Expand Down
19 changes: 2 additions & 17 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import numpy as np

from pandas._config import using_copy_on_write

from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level

Expand Down Expand Up @@ -370,13 +368,6 @@ def concat(
0 1 2
1 3 4
"""
if copy is None:
if using_copy_on_write():
copy = False
else:
copy = True
elif copy and using_copy_on_write():
copy = False

op = _Concatenator(
objs,
Expand All @@ -387,7 +378,6 @@ def concat(
levels=levels,
names=names,
verify_integrity=verify_integrity,
copy=copy,
sort=sort,
)

Expand All @@ -411,7 +401,6 @@ def __init__(
names: list[HashableT] | None = None,
ignore_index: bool = False,
verify_integrity: bool = False,
copy: bool = True,
sort: bool = False,
) -> None:
if isinstance(objs, (ABCSeries, ABCDataFrame, str)):
Expand Down Expand Up @@ -439,7 +428,6 @@ def __init__(

self.ignore_index = ignore_index
self.verify_integrity = verify_integrity
self.copy = copy

objs, keys = self._clean_keys_and_objs(objs, keys)

Expand Down Expand Up @@ -656,7 +644,7 @@ def get_result(self):
cons = sample._constructor_expanddim

index, columns = self.new_axes
df = cons(data, index=index, copy=self.copy)
df = cons(data, index=index, copy=False)
df.columns = columns
return df.__finalize__(self, method="concat")

Expand All @@ -681,10 +669,8 @@ def get_result(self):
mgrs_indexers.append((obj._mgr, indexers))

new_data = concatenate_managers(
mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=self.copy
mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=False
)
if not self.copy and not using_copy_on_write():
new_data._consolidate_inplace()

out = sample._constructor_from_mgr(new_data, axes=new_data.axes)
return out.__finalize__(self, method="concat")
Expand All @@ -710,7 +696,6 @@ def _get_comb_axis(self, i: AxisInt) -> Index:
axis=data_axis,
intersect=self.intersect,
sort=self.sort,
copy=self.copy,
)

@cache_readonly
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

import numpy as np

from pandas._config import using_copy_on_write

from pandas._libs import lib
from pandas._libs.parsers import STR_NA_VALUES
from pandas.errors import (
Expand Down Expand Up @@ -1967,7 +1965,7 @@ def read(self, nrows: int | None = None) -> DataFrame:
new_col_dict,
columns=columns,
index=index,
copy=not using_copy_on_write(),
copy=False,
)

self._currow += new_rows
Expand Down
10 changes: 2 additions & 8 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from pandas._config import (
config,
get_option,
using_copy_on_write,
using_pyarrow_string_dtype,
)

Expand Down Expand Up @@ -3294,13 +3293,8 @@ def read(
dfs.append(df)

if len(dfs) > 0:
out = concat(dfs, axis=1, copy=True)
if using_copy_on_write():
# with CoW, concat ignores the copy keyword. Here, we still want
# to copy to enforce optimized column-major layout
out = out.copy()
out = out.reindex(columns=items, copy=False)
return out
out = concat(dfs, axis=1).copy()
return out.reindex(columns=items, copy=False)

return DataFrame(columns=axes[0], index=axes[1])

Expand Down