Skip to content

Commit fc60380

Browse files
authored
CoW: Remove remaining occurrences from CoW (#57324)
* CoW: Remove remaining occurrences from CoW * Fixup failing tests
1 parent 20e48fe commit fc60380

File tree

8 files changed

+18
-61
lines changed

8 files changed

+18
-61
lines changed

pandas/core/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import numpy as np
2020

21-
from pandas._config import using_copy_on_write
22-
2321
from pandas._libs import lib
2422
from pandas._typing import (
2523
AxisInt,
@@ -661,10 +659,10 @@ def to_numpy(
661659

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

664-
if (copy and not fillna) or (not copy and using_copy_on_write()):
662+
if (copy and not fillna) or not copy:
665663
if np.shares_memory(self._values[:2], result[:2]):
666664
# Take slices to improve performance of check
667-
if using_copy_on_write() and not copy:
665+
if not copy:
668666
result = result.view()
669667
result.flags.writeable = False
670668
else:

pandas/core/indexes/api.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def get_objs_combined_axis(
7474
intersect: bool = False,
7575
axis: Axis = 0,
7676
sort: bool = True,
77-
copy: bool = False,
7877
) -> Index:
7978
"""
8079
Extract combined index: return intersection or union (depending on the
@@ -92,15 +91,13 @@ def get_objs_combined_axis(
9291
The axis to extract indexes from.
9392
sort : bool, default True
9493
Whether the result index should come out sorted or not.
95-
copy : bool, default False
96-
If True, return a copy of the combined index.
9794
9895
Returns
9996
-------
10097
Index
10198
"""
10299
obs_idxes = [obj._get_axis(axis) for obj in objs]
103-
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)
100+
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)
104101

105102

106103
def _get_distinct_objs(objs: list[Index]) -> list[Index]:
@@ -121,7 +118,6 @@ def _get_combined_index(
121118
indexes: list[Index],
122119
intersect: bool = False,
123120
sort: bool = False,
124-
copy: bool = False,
125121
) -> Index:
126122
"""
127123
Return the union or intersection of indexes.
@@ -135,8 +131,6 @@ def _get_combined_index(
135131
calculate the union.
136132
sort : bool, default False
137133
Whether the result index should come out sorted or not.
138-
copy : bool, default False
139-
If True, return a copy of the combined index.
140134
141135
Returns
142136
-------
@@ -158,10 +152,6 @@ def _get_combined_index(
158152

159153
if sort:
160154
index = safe_sort_index(index)
161-
# GH 29879
162-
if copy:
163-
index = index.copy()
164-
165155
return index
166156

167157

pandas/core/indexes/base.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from pandas._config import (
2424
get_option,
25-
using_copy_on_write,
2625
using_pyarrow_string_dtype,
2726
)
2827

@@ -1633,7 +1632,7 @@ def to_frame(
16331632

16341633
if name is lib.no_default:
16351634
name = self._get_level_names()
1636-
result = DataFrame({name: self}, copy=not using_copy_on_write())
1635+
result = DataFrame({name: self}, copy=False)
16371636

16381637
if index:
16391638
result.index = self
@@ -4773,13 +4772,11 @@ def values(self) -> ArrayLike:
47734772
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
47744773
Length: 5, dtype: interval[int64, right]
47754774
"""
4776-
if using_copy_on_write():
4777-
data = self._data
4778-
if isinstance(data, np.ndarray):
4779-
data = data.view()
4780-
data.flags.writeable = False
4781-
return data
4782-
return self._data
4775+
data = self._data
4776+
if isinstance(data, np.ndarray):
4777+
data = data.view()
4778+
data.flags.writeable = False
4779+
return data
47834780

47844781
@cache_readonly
47854782
@doc(IndexOpsMixin.array)

pandas/core/indexes/datetimelike.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import numpy as np
1818

19-
from pandas._config import using_copy_on_write
20-
2119
from pandas._libs import (
2220
NaT,
2321
Timedelta,
@@ -454,9 +452,8 @@ def _with_freq(self, freq):
454452
def values(self) -> np.ndarray:
455453
# NB: For Datetime64TZ this is lossy
456454
data = self._data._ndarray
457-
if using_copy_on_write():
458-
data = data.view()
459-
data.flags.writeable = False
455+
data = data.view()
456+
data.flags.writeable = False
460457
return data
461458

462459
@doc(DatetimeIndexOpsMixin.shift)

pandas/core/indexing.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import numpy as np
1515

16-
from pandas._config import using_copy_on_write
17-
1816
from pandas._libs.indexing import NDFrameIndexerBase
1917
from pandas._libs.lib import item_from_zerodim
2018
from pandas.compat import PYPY
@@ -894,7 +892,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:
894892

895893
@final
896894
def __setitem__(self, key, value) -> None:
897-
if not PYPY and using_copy_on_write():
895+
if not PYPY:
898896
if sys.getrefcount(self.obj) <= 2:
899897
warnings.warn(
900898
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
@@ -2107,7 +2105,7 @@ def _setitem_with_indexer_frame_value(
21072105
tuple(sub_indexer),
21082106
value[item],
21092107
multiindex_indexer,
2110-
using_cow=using_copy_on_write(),
2108+
using_cow=True,
21112109
)
21122110
else:
21132111
val = np.nan

pandas/core/reshape/concat.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
import numpy as np
1717

18-
from pandas._config import using_copy_on_write
19-
2018
from pandas.util._decorators import cache_readonly
2119
from pandas.util._exceptions import find_stack_level
2220

@@ -370,13 +368,6 @@ def concat(
370368
0 1 2
371369
1 3 4
372370
"""
373-
if copy is None:
374-
if using_copy_on_write():
375-
copy = False
376-
else:
377-
copy = True
378-
elif copy and using_copy_on_write():
379-
copy = False
380371

381372
op = _Concatenator(
382373
objs,
@@ -387,7 +378,6 @@ def concat(
387378
levels=levels,
388379
names=names,
389380
verify_integrity=verify_integrity,
390-
copy=copy,
391381
sort=sort,
392382
)
393383

@@ -411,7 +401,6 @@ def __init__(
411401
names: list[HashableT] | None = None,
412402
ignore_index: bool = False,
413403
verify_integrity: bool = False,
414-
copy: bool = True,
415404
sort: bool = False,
416405
) -> None:
417406
if isinstance(objs, (ABCSeries, ABCDataFrame, str)):
@@ -439,7 +428,6 @@ def __init__(
439428

440429
self.ignore_index = ignore_index
441430
self.verify_integrity = verify_integrity
442-
self.copy = copy
443431

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

@@ -656,7 +644,7 @@ def get_result(self):
656644
cons = sample._constructor_expanddim
657645

658646
index, columns = self.new_axes
659-
df = cons(data, index=index, copy=self.copy)
647+
df = cons(data, index=index, copy=False)
660648
df.columns = columns
661649
return df.__finalize__(self, method="concat")
662650

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

683671
new_data = concatenate_managers(
684-
mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=self.copy
672+
mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=False
685673
)
686-
if not self.copy and not using_copy_on_write():
687-
new_data._consolidate_inplace()
688674

689675
out = sample._constructor_from_mgr(new_data, axes=new_data.axes)
690676
return out.__finalize__(self, method="concat")
@@ -710,7 +696,6 @@ def _get_comb_axis(self, i: AxisInt) -> Index:
710696
axis=data_axis,
711697
intersect=self.intersect,
712698
sort=self.sort,
713-
copy=self.copy,
714699
)
715700

716701
@cache_readonly

pandas/io/parsers/readers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import numpy as np
2828

29-
from pandas._config import using_copy_on_write
30-
3129
from pandas._libs import lib
3230
from pandas._libs.parsers import STR_NA_VALUES
3331
from pandas.errors import (
@@ -1967,7 +1965,7 @@ def read(self, nrows: int | None = None) -> DataFrame:
19671965
new_col_dict,
19681966
columns=columns,
19691967
index=index,
1970-
copy=not using_copy_on_write(),
1968+
copy=False,
19711969
)
19721970

19731971
self._currow += new_rows

pandas/io/pytables.py

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

@@ -3294,13 +3293,8 @@ def read(
32943293
dfs.append(df)
32953294

32963295
if len(dfs) > 0:
3297-
out = concat(dfs, axis=1, copy=True)
3298-
if using_copy_on_write():
3299-
# with CoW, concat ignores the copy keyword. Here, we still want
3300-
# to copy to enforce optimized column-major layout
3301-
out = out.copy()
3302-
out = out.reindex(columns=items, copy=False)
3303-
return out
3296+
out = concat(dfs, axis=1).copy()
3297+
return out.reindex(columns=items, copy=False)
33043298

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

0 commit comments

Comments
 (0)