diff --git a/pandas/core/base.py b/pandas/core/base.py index a1484d9ad032b..cb8848db9af60 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -218,7 +218,7 @@ def _obj_with_exclusions(self): return self.obj if self._selection is not None: - return self.obj._getitem_nocopy(self._selection_list) + return self.obj[self._selection_list] if len(self.exclusions) > 0: # equivalent to `self.obj.drop(self.exclusions, axis=1) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 771554f2b0f2b..85e13401d6e32 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1676,8 +1676,8 @@ def dot(self, other: AnyArrayLike | DataFrame) -> DataFrame | Series: if len(common) > len(self.columns) or len(common) > len(other.index): raise ValueError("matrices are not aligned") - left = self.reindex(columns=common, copy=False) - right = other.reindex(index=common, copy=False) + left = self.reindex(columns=common) + right = other.reindex(index=common) lvals = left.values rvals = right._values else: @@ -3800,27 +3800,6 @@ def _iter_column_arrays(self) -> Iterator[ArrayLike]: for i in range(len(self.columns)): yield self._get_column_array(i) - def _getitem_nocopy(self, key: list): - """ - Behaves like __getitem__, but returns a view in cases where __getitem__ - would make a copy. - """ - # TODO(CoW): can be removed if/when we are always Copy-on-Write - indexer = self.columns._get_indexer_strict(key, "columns")[1] - new_axis = self.columns[indexer] - - new_mgr = self._mgr.reindex_indexer( - new_axis, - indexer, - axis=0, - allow_dups=True, - copy=False, - only_slice=True, - ) - result = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes) - result = result.__finalize__(self) - return result - def __getitem__(self, key): check_dict_or_set_indexers(key) key = lib.item_from_zerodim(key) @@ -3911,7 +3890,7 @@ def _getitem_bool_array(self, key): key = check_bool_indexer(self.index, key) if key.all(): - return self.copy(deep=None) + return self.copy(deep=False) indexer = key.nonzero()[0] return self.take(indexer, axis=0) @@ -4774,7 +4753,7 @@ def predicate(arr: ArrayLike) -> bool: return True - mgr = self._mgr._get_data_subset(predicate).copy(deep=None) + mgr = self._mgr._get_data_subset(predicate).copy(deep=False) return self._constructor_from_mgr(mgr, axes=mgr.axes).__finalize__(self) def insert( @@ -4919,7 +4898,7 @@ def assign(self, **kwargs) -> DataFrame: Portland 17.0 62.6 290.15 Berkeley 25.0 77.0 298.15 """ - data = self.copy(deep=None) + data = self.copy(deep=False) for k, v in kwargs.items(): data[k] = com.apply_if_callable(v, data) @@ -4996,7 +4975,6 @@ def _reindex_multi(self, axes: dict[str, Index], fill_value) -> DataFrame: else: return self._reindex_with_indexers( {0: [new_index, row_indexer], 1: [new_columns, col_indexer]}, - copy=False, fill_value=fill_value, ) @@ -5038,7 +5016,7 @@ def set_axis( axis: Axis = 0, copy: bool | None = None, ) -> DataFrame: - return super().set_axis(labels, axis=axis, copy=copy) + return super().set_axis(labels, axis=axis) @doc( NDFrame.reindex, @@ -5065,7 +5043,6 @@ def reindex( columns=columns, axis=axis, method=method, - copy=copy, level=level, fill_value=fill_value, limit=limit, @@ -5463,7 +5440,6 @@ def rename( index=index, columns=columns, axis=axis, - copy=copy, inplace=inplace, level=level, errors=errors, @@ -5534,7 +5510,7 @@ def _replace_columnwise( DataFrame or None """ # Operate column-wise - res = self if inplace else self.copy(deep=None) + res = self if inplace else self.copy(deep=False) ax = self.columns for i, ax_value in enumerate(ax): @@ -5823,8 +5799,7 @@ def set_index( if inplace: frame = self else: - # GH 49473 Use "lazy copy" with Copy-on-Write - frame = self.copy(deep=None) + frame = self.copy(deep=False) arrays: list[Index] = [] names: list[Hashable] = [] @@ -6114,7 +6089,7 @@ class max type if inplace: new_obj = self else: - new_obj = self.copy(deep=None) + new_obj = self.copy(deep=False) if allow_duplicates is not lib.no_default: allow_duplicates = validate_bool_kwarg(allow_duplicates, "allow_duplicates") @@ -6386,7 +6361,7 @@ def dropna( raise ValueError(f"invalid how option: {how}") if np.all(mask): - result = self.copy(deep=None) + result = self.copy(deep=False) else: result = self.loc(axis=axis)[mask] @@ -6515,7 +6490,7 @@ def drop_duplicates( 4 Indomie pack 5.0 """ if self.empty: - return self.copy(deep=None) + return self.copy(deep=False) inplace = validate_bool_kwarg(inplace, "inplace") ignore_index = validate_bool_kwarg(ignore_index, "ignore_index") @@ -6631,7 +6606,7 @@ def duplicated( def f(vals) -> tuple[np.ndarray, int]: labels, shape = algorithms.factorize(vals, size_hint=len(self)) - return labels.astype("i8", copy=False), len(shape) + return labels.astype("i8"), len(shape) if subset is None: # https://github.com/pandas-dev/pandas/issues/28770 @@ -6914,7 +6889,7 @@ def sort_values( if inplace: return self._update_inplace(self) else: - return self.copy(deep=None) + return self.copy(deep=False) if is_range_indexer(indexer, len(indexer)): result = self.copy(deep=False) @@ -7570,7 +7545,7 @@ def nsmallest( ), ) def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame: - result = self.copy(deep=None) + result = self.copy(deep=False) axis = self._get_axis_number(axis) @@ -7630,7 +7605,7 @@ class diet if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover raise TypeError("Can only reorder levels on a hierarchical axis.") - result = self.copy(deep=None) + result = self.copy(deep=False) if axis == 0: assert isinstance(result.index, MultiIndex) @@ -7933,9 +7908,7 @@ def to_series(right): if flex is not None and isinstance(right, DataFrame): if not left._indexed_same(right): if flex: - left, right = left.align( - right, join="outer", level=level, copy=False - ) + left, right = left.align(right, join="outer", level=level) else: raise ValueError( "Can only compare identically-labeled (both index and columns) " @@ -7948,7 +7921,7 @@ def to_series(right): if not left.axes[axis].equals(right.index): raise ValueError( "Operands are not aligned. Do " - "`left, right = left.align(right, axis=1, copy=False)` " + "`left, right = left.align(right, axis=1)` " "before operating." ) @@ -7957,7 +7930,6 @@ def to_series(right): join="outer", axis=axis, level=level, - copy=False, ) right = left._maybe_align_series_as_frame(right, axis) @@ -8467,7 +8439,7 @@ def combine( """ other_idxlen = len(other.index) # save for compare - this, other = self.align(other, copy=False) + this, other = self.align(other) new_index = this.index if other.empty and len(new_index) == len(self.index): @@ -8507,15 +8479,15 @@ def combine( # try to promote series, which is all NaN, as other_dtype. new_dtype = other_dtype try: - series = series.astype(new_dtype, copy=False) + series = series.astype(new_dtype) except ValueError: # e.g. new_dtype is integer types pass else: # if we have different dtypes, possibly promote new_dtype = find_common_type([this_dtype, other_dtype]) - series = series.astype(new_dtype, copy=False) - other_series = other_series.astype(new_dtype, copy=False) + series = series.astype(new_dtype) + other_series = other_series.astype(new_dtype) arr = func(series, other_series) if isinstance(new_dtype, np.dtype): @@ -9567,7 +9539,7 @@ def explode( result.index = default_index(len(result)) else: result.index = self.index.take(result.index) - result = result.reindex(columns=self.columns, copy=False) + result = result.reindex(columns=self.columns) return result.__finalize__(self, method="explode") @@ -10263,9 +10235,7 @@ def _append( row_df = other.to_frame().T # infer_objects is needed for # test_append_empty_frame_to_series_with_dateutil_tz - other = row_df.infer_objects(copy=False).rename_axis( - index.names, copy=False - ) + other = row_df.infer_objects().rename_axis(index.names) elif isinstance(other, list): if not other: pass @@ -10509,7 +10479,7 @@ def join( res = concat( frames, axis=1, join="outer", verify_integrity=True, sort=sort ) - return res.reindex(self.index, copy=False) + return res.reindex(self.index) else: return concat( frames, axis=1, join=how, verify_integrity=True, sort=sort @@ -10559,7 +10529,6 @@ def merge( right_index=right_index, sort=sort, suffixes=suffixes, - copy=copy, indicator=indicator, validate=validate, ) @@ -11024,7 +10993,7 @@ def corrwith( if numeric_only: other = other._get_numeric_data() - left, right = this.align(other, join="inner", copy=False) + left, right = this.align(other, join="inner") if axis == 1: left = left.T @@ -11161,7 +11130,7 @@ def count(self, axis: Axis = 0, numeric_only: bool = False): else: result = notna(frame).sum(axis=axis) - return result.astype("int64", copy=False).__finalize__(self, method="count") + return result.astype("int64").__finalize__(self, method="count") def _reduce( self, @@ -11225,7 +11194,7 @@ def _get_data() -> DataFrame: if axis is None: dtype = find_common_type([arr.dtype for arr in df._mgr.arrays]) if isinstance(dtype, ExtensionDtype): - df = df.astype(dtype, copy=False) + df = df.astype(dtype) arr = concat_compat(list(df._iter_column_arrays())) return arr._reduce(name, skipna=skipna, keepdims=False, **kwds) return func(df.values) @@ -11257,7 +11226,7 @@ def _get_data() -> DataFrame: # be equivalent to transposing the original frame and aggregating # with axis=0. name = {"argmax": "idxmax", "argmin": "idxmin"}.get(name, name) - df = df.astype(dtype, copy=False) + df = df.astype(dtype) arr = concat_compat(list(df._iter_column_arrays())) nrows, ncols = df.shape row_index = np.tile(np.arange(nrows), ncols) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3502754eb4aff..8e0767d9b3699 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -735,15 +735,13 @@ def set_axis( -------- %(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s. """ - return self._set_axis_nocheck(labels, axis, inplace=False, copy=copy) + return self._set_axis_nocheck(labels, axis, inplace=False) @final - def _set_axis_nocheck(self, labels, axis: Axis, inplace: bool, copy: bool | None): + def _set_axis_nocheck(self, labels, axis: Axis, inplace: bool): if inplace: setattr(self, self._get_axis_name(axis), labels) else: - # With copy=False, we create a new object but don't copy the - # underlying data. obj = self.copy(deep=False) setattr(obj, obj._get_axis_name(axis), labels) return obj @@ -880,7 +878,7 @@ def droplevel(self, level: IndexLabel, axis: Axis = 0) -> Self: """ labels = self._get_axis(axis) new_labels = labels.droplevel(level) - return self.set_axis(new_labels, axis=axis, copy=None) + return self.set_axis(new_labels, axis=axis) def pop(self, item: Hashable) -> Series | Any: result = self[item] @@ -1069,7 +1067,7 @@ def _rename( raise KeyError(f"{missing_labels} not found in axis") new_index = ax._transform_index(f, level=level) - result._set_axis_nocheck(new_index, axis=axis_no, inplace=True, copy=False) + result._set_axis_nocheck(new_index, axis=axis_no, inplace=True) if inplace: self._update_inplace(result) @@ -4137,9 +4135,7 @@ def _getitem_slice(self, key: slice) -> Self: slobj = self.index._convert_slice_indexer(key, kind="getitem") if isinstance(slobj, np.ndarray): # reachable with DatetimeIndex - indexer = lib.maybe_indices_to_slice( - slobj.astype(np.intp, copy=False), len(self) - ) + indexer = lib.maybe_indices_to_slice(slobj.astype(np.intp), len(self)) if isinstance(indexer, np.ndarray): # GH#43223 If we can not convert, use take return self.take(indexer, axis=0) @@ -4385,7 +4381,6 @@ def reindex_like( d = other._construct_axes_dict( axes=self._AXIS_ORDERS, method=method, - copy=copy, limit=limit, tolerance=tolerance, ) @@ -4551,7 +4546,6 @@ def _drop_axis( indexer, axis=bm_axis, allow_dups=True, - copy=None, only_slice=only_slice, ) result = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes) @@ -4992,7 +4986,7 @@ def sort_index( if inplace: result = self else: - result = self.copy(deep=None) + result = self.copy(deep=False) if ignore_index: if axis == 1: @@ -5290,7 +5284,7 @@ def reindex( # perform the reindex on the axes return self._reindex_axes( - axes, level, limit, tolerance, method, fill_value, False + axes, level, limit, tolerance, method, fill_value ).__finalize__(self, method="reindex") @final @@ -5302,7 +5296,6 @@ def _reindex_axes( tolerance, method, fill_value: Scalar | None, - copy: bool | None, ) -> Self: """Perform the reindex for all the axes.""" obj = self @@ -5320,11 +5313,8 @@ def _reindex_axes( obj = obj._reindex_with_indexers( {axis: [new_index, indexer]}, fill_value=fill_value, - copy=copy, allow_dups=False, ) - # If we've made a copy once, no need to make another one - copy = False return obj @@ -5347,7 +5337,6 @@ def _reindex_with_indexers( self, reindexers, fill_value=None, - copy: bool | None = False, allow_dups: bool = False, ) -> Self: """allow_dups indicates an internal call here""" @@ -5371,10 +5360,7 @@ def _reindex_with_indexers( axis=baxis, fill_value=fill_value, allow_dups=allow_dups, - copy=copy, ) - # If we've made a copy once, no need to make another one - copy = False if new_data is self._mgr: new_data = new_data.copy(deep=False) @@ -6307,7 +6293,7 @@ def astype( f"'{col_name}' not found in columns." ) - dtype_ser = dtype_ser.reindex(self.columns, fill_value=None, copy=False) + dtype_ser = dtype_ser.reindex(self.columns, fill_value=None) results = [] for i, (col_name, col) in enumerate(self.items()): @@ -7000,11 +6986,11 @@ def fillna( # test_fillna_nonscalar if inplace: return None - return self.copy(deep=None) + return self.copy(deep=False) from pandas import Series value = Series(value) - value = value.reindex(self.index, copy=False) + value = value.reindex(self.index) value = value._values elif not is_list_like(value): pass @@ -9820,7 +9806,6 @@ def align( join=join, axis=axis, level=level, - copy=copy, fill_value=fill_value, method=method, limit=limit, @@ -9840,7 +9825,6 @@ def align( join=join, axis=axis, level=level, - copy=copy, fill_value=fill_value, method=method, limit=limit, @@ -9856,7 +9840,6 @@ def align( join=join, axis=axis, level=level, - copy=copy, fill_value=fill_value, method=method, limit=limit, @@ -9869,7 +9852,6 @@ def align( join=join, axis=axis, level=level, - copy=copy, fill_value=fill_value, method=method, limit=limit, @@ -9903,7 +9885,6 @@ def _align_frame( join: AlignJoin = "outer", axis: Axis | None = None, level=None, - copy: bool | None = None, fill_value=None, method=None, limit: int | None = None, @@ -9936,12 +9917,11 @@ def _align_frame( reindexers = {0: [join_index, ilidx], 1: [join_columns, clidx]} left = self._reindex_with_indexers( - reindexers, copy=copy, fill_value=fill_value, allow_dups=True + reindexers, fill_value=fill_value, allow_dups=True ) # other must be always DataFrame right = other._reindex_with_indexers( {0: [join_index, iridx], 1: [join_columns, cridx]}, - copy=copy, fill_value=fill_value, allow_dups=True, ) @@ -9959,14 +9939,12 @@ def _align_series( join: AlignJoin = "outer", axis: Axis | None = None, level=None, - copy: bool | None = None, fill_value=None, method=None, limit: int | None = None, fill_axis: Axis = 0, ) -> tuple[Self, Series, Index | None]: is_series = isinstance(self, ABCSeries) - copy = False if (not is_series and axis is None) or axis not in [None, 0, 1]: raise ValueError("Must specify axis=0 or 1") @@ -9987,9 +9965,9 @@ def _align_series( if is_series: left = self._reindex_indexer(join_index, lidx) elif lidx is None or join_index is None: - left = self.copy(deep=copy) + left = self.copy(deep=False) else: - new_mgr = self._mgr.reindex_indexer(join_index, lidx, axis=1, copy=copy) + new_mgr = self._mgr.reindex_indexer(join_index, lidx, axis=1) left = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes) right = other._reindex_indexer(join_index, ridx) @@ -10008,13 +9986,10 @@ def _align_series( bm_axis = self._get_block_manager_axis(1) fdata = fdata.reindex_indexer(join_index, lidx, axis=bm_axis) - if copy and fdata is self._mgr: - fdata = fdata.copy() - left = self._constructor_from_mgr(fdata, axes=fdata.axes) if ridx is None: - right = other.copy(deep=copy) + right = other.copy(deep=False) else: right = other.reindex(join_index, level=level) @@ -10059,7 +10034,7 @@ def _where( copy=False, ) cond.columns = self.columns - cond = cond.align(self, join="right", copy=False)[0] + cond = cond.align(self, join="right")[0] else: if not hasattr(cond, "shape"): cond = np.asanyarray(cond) @@ -10076,7 +10051,7 @@ def _where( category=FutureWarning, ) cond = cond.fillna(fill_value) - cond = cond.infer_objects(copy=False) + cond = cond.infer_objects() msg = "Boolean array expected for the condition, not {dtype}" @@ -10100,7 +10075,7 @@ def _where( cond = cond.astype(bool) cond = -cond if inplace else cond - cond = cond.reindex(self._info_axis, axis=self._info_axis_number, copy=False) + cond = cond.reindex(self._info_axis, axis=self._info_axis_number) # try to align with other if isinstance(other, NDFrame): @@ -10113,7 +10088,6 @@ def _where( axis=axis, level=level, fill_value=None, - copy=False, )[1] # if we are NOT aligned, raise as we cannot where index @@ -10925,7 +10899,7 @@ def _tz_convert(ax, tz): ax = _tz_convert(ax, tz) result = self.copy(deep=False) - result = result.set_axis(ax, axis=axis, copy=False) + result = result.set_axis(ax, axis=axis) return result.__finalize__(self, method="tz_convert") @final @@ -11131,7 +11105,7 @@ def _tz_localize(ax, tz, ambiguous, nonexistent): ax = _tz_localize(ax, tz, ambiguous, nonexistent) result = self.copy(deep=False) - result = result.set_axis(ax, axis=axis, copy=False) + result = result.set_axis(ax, axis=axis) return result.__finalize__(self, method="tz_localize") # ---------------------------------------------------------------------- diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index afd37ca684a08..c10ea9636cba5 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1937,9 +1937,7 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT: # Don't convert indices: negative indices need to give rise # to null values in the result new_ax = result.index.take(ids) - output = result._reindex_with_indexers( - {0: (new_ax, ids)}, allow_dups=True, copy=False - ) + output = result._reindex_with_indexers({0: (new_ax, ids)}, allow_dups=True) output = output.set_axis(obj.index, axis=0) return output diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 4ccac6449d835..b127f62faf827 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1330,7 +1330,7 @@ def _multi_take(self, tup: tuple): axis: self._get_listlike_indexer(key, axis) for (key, axis) in zip(tup, self.obj._AXIS_ORDERS) } - return self.obj._reindex_with_indexers(d, copy=True, allow_dups=True) + return self.obj._reindex_with_indexers(d, allow_dups=True) # ------------------------------------------------------------------- @@ -1362,7 +1362,7 @@ def _getitem_iterable(self, key, axis: AxisInt): # A collection of keys keyarr, indexer = self._get_listlike_indexer(key, axis) return self.obj._reindex_with_indexers( - {axis: [keyarr, indexer]}, copy=True, allow_dups=True + {axis: [keyarr, indexer]}, allow_dups=True ) def _getitem_tuple(self, tup: tuple): diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index df5c0112cfdd1..af529c837089a 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -16,10 +16,7 @@ import numpy as np -from pandas._config import ( - get_option, - using_copy_on_write, -) +from pandas._config import get_option from pandas._libs import ( NaT, @@ -2612,7 +2609,7 @@ def external_values(values: ArrayLike) -> ArrayLike: # Avoid raising in .astype in casting from dt64tz to dt64 values = values._ndarray - if isinstance(values, np.ndarray) and using_copy_on_write(): + if isinstance(values, np.ndarray): values = values.view() values.flags.writeable = False diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index af16a4175319d..d833dab5b820f 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -185,7 +185,6 @@ def _maybe_reindex_columns_na_proxy( axes[i], indexers[i], axis=i, - copy=False, only_slice=True, # only relevant for i==0 allow_dups=True, use_na_proxy=True, # only relevant for i==0 diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index d3ea5b4af3af1..4e05da8ae9c98 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -19,8 +19,6 @@ import numpy as np -from pandas._config import using_copy_on_write - from pandas._libs import ( algos as libalgos, internals as libinternals, @@ -599,7 +597,7 @@ def diff(self, n: int) -> Self: # only reached with self.ndim == 2 return self.apply("diff", n=n) - def astype(self, dtype, copy: bool | None = False, errors: str = "raise") -> Self: + def astype(self, dtype, errors: str = "raise") -> Self: return self.apply("astype", dtype=dtype, errors=errors) def convert(self) -> Self: @@ -720,14 +718,7 @@ def copy(self, deep: bool | None | Literal["all"] = True) -> Self: ------- BlockManager """ - if deep is None: - if using_copy_on_write(): - # use shallow copy - deep = False - else: - # preserve deep copy for BlockManager with copy=None - deep = True - + deep = deep if deep is not None else False # this preserves the notion of view copying of axes if deep: # hit in e.g. tests.io.json.test_pandas @@ -793,7 +784,6 @@ def reindex_axis( indexer, axis=axis, fill_value=fill_value, - copy=False, only_slice=only_slice, ) @@ -804,7 +794,6 @@ def reindex_indexer( axis: AxisInt, fill_value=None, allow_dups: bool = False, - copy: bool | None = True, only_slice: bool = False, *, use_na_proxy: bool = False, @@ -817,8 +806,6 @@ def reindex_indexer( axis : int fill_value : object, default None allow_dups : bool, default False - copy : bool or None, default True - If None, regard as False to get shallow copy. only_slice : bool, default False Whether to take views, not copies, along columns. use_na_proxy : bool, default False @@ -826,19 +813,11 @@ def reindex_indexer( pandas-indexer with -1's only. """ - if copy is None: - if using_copy_on_write(): - # use shallow copy - copy = False - else: - # preserve deep copy for BlockManager with copy=None - copy = True - if indexer is None: - if new_axis is self.axes[axis] and not copy: + if new_axis is self.axes[axis]: return self - result = self.copy(deep=copy) + result = self.copy(deep=False) result.axes = list(self.axes) result.axes[axis] = new_axis return result @@ -1076,7 +1055,6 @@ def take( indexer=indexer, axis=axis, allow_dups=True, - copy=None, ) @@ -1463,10 +1441,7 @@ def _iset_single( # Caller is responsible for verifying value.shape if inplace and blk.should_store(value): - copy = False - if not self._has_no_reference_block(blkno): - # perform Copy-on-Write and clear the reference - copy = True + copy = not self._has_no_reference_block(blkno) iloc = self.blklocs[loc] blk.set_inplace(slice(iloc, iloc + 1), value, copy=copy) return diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 4f10fd729723e..0494138d1e16f 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -163,7 +163,6 @@ def merge( suffixes=suffixes, indicator=indicator, validate=validate, - copy=copy, ) else: op = _MergeOperation( @@ -180,7 +179,7 @@ def merge( indicator=indicator, validate=validate, ) - return op.get_result(copy=copy) + return op.get_result() def _cross_merge( @@ -193,7 +192,6 @@ def _cross_merge( right_index: bool = False, sort: bool = False, suffixes: Suffixes = ("_x", "_y"), - copy: bool | None = None, indicator: str | bool = False, validate: str | None = None, ) -> DataFrame: @@ -232,7 +230,6 @@ def _cross_merge( suffixes=suffixes, indicator=indicator, validate=validate, - copy=copy, ) del res[cross_col] return res @@ -291,7 +288,7 @@ def _groupby_and_merge( from pandas.core.reshape.concat import concat result = concat(pieces, ignore_index=True) - result = result.reindex(columns=pieces[0].columns, copy=False) + result = result.reindex(columns=pieces[0].columns) return result, lby @@ -708,7 +705,6 @@ def merge_asof( # TODO: transformations?? -# TODO: only copy DataFrames when modification necessary class _MergeOperation: """ Perform a database (SQL) merge operation between two DataFrame or Series @@ -726,7 +722,6 @@ class _MergeOperation: right_index: bool sort: bool suffixes: Suffixes - copy: bool indicator: str | bool validate: str | None join_names: list[Hashable] @@ -827,7 +822,6 @@ def _reindex_and_concat( join_index: Index, left_indexer: npt.NDArray[np.intp] | None, right_indexer: npt.NDArray[np.intp] | None, - copy: bool | None, ) -> DataFrame: """ reindex along index and concat along columns. @@ -848,7 +842,6 @@ def _reindex_and_concat( join_index, left_indexer, axis=1, - copy=False, only_slice=True, allow_dups=True, use_na_proxy=True, @@ -863,7 +856,6 @@ def _reindex_and_concat( join_index, right_indexer, axis=1, - copy=False, only_slice=True, allow_dups=True, use_na_proxy=True, @@ -875,18 +867,16 @@ def _reindex_and_concat( left.columns = llabels right.columns = rlabels - result = concat([left, right], axis=1, copy=copy) + result = concat([left, right], axis=1) return result - def get_result(self, copy: bool | None = True) -> DataFrame: + def get_result(self) -> DataFrame: if self.indicator: self.left, self.right = self._indicator_pre_merge(self.left, self.right) join_index, left_indexer, right_indexer = self._get_join_info() - result = self._reindex_and_concat( - join_index, left_indexer, right_indexer, copy=copy - ) + result = self._reindex_and_concat(join_index, left_indexer, right_indexer) result = result.__finalize__(self, method=self._merge_type) if self.indicator: @@ -1920,7 +1910,7 @@ def __init__( sort=True, # factorize sorts ) - def get_result(self, copy: bool | None = True) -> DataFrame: + def get_result(self) -> DataFrame: join_index, left_indexer, right_indexer = self._get_join_info() left_join_indexer: npt.NDArray[np.intp] | None @@ -1942,7 +1932,7 @@ def get_result(self, copy: bool | None = True) -> DataFrame: raise ValueError("fill_method must be 'ffill' or None") result = self._reindex_and_concat( - join_index, left_join_indexer, right_join_indexer, copy=copy + join_index, left_join_indexer, right_join_indexer ) self._maybe_add_join_keys(result, left_indexer, right_indexer) @@ -2308,7 +2298,7 @@ def _get_multiindex_indexer( if sort: rcodes = list(map(np.take, rcodes, index.codes)) else: - i8copy = lambda a: a.astype("i8", subok=False, copy=True) + i8copy = lambda a: a.astype("i8", subok=False) rcodes = list(map(i8copy, index.codes)) # fix right labels if there were any nulls diff --git a/pandas/core/series.py b/pandas/core/series.py index 65ef7d352a547..df7b6b8f1ab12 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -25,8 +25,6 @@ import numpy as np -from pandas._config import using_copy_on_write - from pandas._libs import ( lib, properties, @@ -397,7 +395,7 @@ def __init__( if copy is None: copy = False - if isinstance(data, SingleBlockManager) and using_copy_on_write() and not copy: + if isinstance(data, SingleBlockManager) and not copy: data = data.copy(deep=False) if not allow_mgr: @@ -432,7 +430,7 @@ def __init__( refs = None if isinstance(data, Index): if dtype is not None: - data = data.astype(dtype, copy=False) + data = data.astype(dtype) refs = data._references data = data._values @@ -451,7 +449,7 @@ def __init__( index = data.index data = data._mgr.copy(deep=False) else: - data = data.reindex(index, copy=copy) + data = data.reindex(index) copy = False data = data._mgr elif is_dict_like(data): @@ -498,7 +496,7 @@ def __init__( # create/copy the manager if isinstance(data, SingleBlockManager): if dtype is not None: - data = data.astype(dtype=dtype, errors="ignore", copy=copy) + data = data.astype(dtype=dtype, errors="ignore") elif copy: data = data.copy() else: @@ -568,7 +566,7 @@ def _init_dict( # Now we just make sure the order is respected, if any if data and index is not None: - s = s.reindex(index, copy=False) + s = s.reindex(index) return s._mgr, s.index # ---------------------------------------------------------------------- @@ -2664,7 +2662,7 @@ def corr( >>> s1.corr(s2) -1.0 """ # noqa: E501 - this, other = self.align(other, join="inner", copy=False) + this, other = self.align(other, join="inner") if len(this) == 0: return np.nan @@ -2721,7 +2719,7 @@ def cov( >>> s1.cov(s2) -0.01685762652715874 """ - this, other = self.align(other, join="inner", copy=False) + this, other = self.align(other, join="inner") if len(this) == 0: return np.nan this_values = this.to_numpy(dtype=float, na_value=np.nan, copy=False) @@ -2923,8 +2921,8 @@ def dot(self, other: AnyArrayLike) -> Series | np.ndarray: if len(common) > len(self.index) or len(common) > len(other.index): raise ValueError("matrices are not aligned") - left = self.reindex(index=common, copy=False) - right = other.reindex(index=common, copy=False) + left = self.reindex(index=common) + right = other.reindex(index=common) lvals = left.values rvals = right.values else: @@ -3235,13 +3233,13 @@ def combine_first(self, other) -> Series: keep_other = other.index.difference(this.index[notna(this)]) keep_this = this.index.difference(keep_other) - this = this.reindex(keep_this, copy=False) - other = other.reindex(keep_other, copy=False) + this = this.reindex(keep_this) + other = other.reindex(keep_other) if this.dtype.kind == "M" and other.dtype.kind != "M": other = to_datetime(other) combined = concat([this, other]) - combined = combined.reindex(new_index, copy=False) + combined = combined.reindex(new_index) return combined.__finalize__(self, method="combine_first") def update(self, other: Series | Sequence | Mapping) -> None: @@ -3552,7 +3550,7 @@ def sort_values( if is_range_indexer(sorted_index, len(sorted_index)): if inplace: return self._update_inplace(self) - return self.copy(deep=None) + return self.copy(deep=False) result = self._constructor( self._values[sorted_index], index=self.index[sorted_index], copy=False @@ -4185,7 +4183,7 @@ def reorder_levels(self, order: Sequence[Level]) -> Series: if not isinstance(self.index, MultiIndex): # pragma: no cover raise Exception("Can only reorder levels on a hierarchical axis.") - result = self.copy(deep=None) + result = self.copy(deep=False) assert isinstance(result.index, MultiIndex) result.index = result.index.reorder_levels(order) return result @@ -4777,7 +4775,6 @@ def rename( # Hashable], Callable[[Any], Hashable], None]" return super()._rename( index, # type: ignore[arg-type] - copy=copy, inplace=inplace, level=level, errors=errors, @@ -4818,7 +4815,7 @@ def set_axis( axis: Axis = 0, copy: bool | None = None, ) -> Series: - return super().set_axis(labels, axis=axis, copy=copy) + return super().set_axis(labels, axis=axis) # error: Cannot determine type of 'reindex' @doc( @@ -4841,7 +4838,6 @@ def reindex( # type: ignore[override] return super().reindex( index=index, method=method, - copy=copy, level=level, fill_value=fill_value, limit=limit, @@ -4958,7 +4954,6 @@ def rename_axis( mapper=mapper, index=index, axis=axis, - copy=copy, inplace=inplace, ) @@ -5488,7 +5483,7 @@ def case_when( ) for condition, replacement in caselist ] - default = self.copy() + default = self.copy(deep=False) conditions, replacements = zip(*caselist) common_dtypes = [infer_dtype_from(arg)[0] for arg in [*replacements, default]] if len(set(common_dtypes)) > 1: @@ -5652,7 +5647,7 @@ def dropna( result = remove_na_arraylike(self) else: if not inplace: - result = self.copy(deep=None) + result = self.copy(deep=False) else: result = self @@ -5913,7 +5908,7 @@ def _align_for_op(self, right, align_asobject: bool = False): left = left.astype(object) right = right.astype(object) - left, right = left.align(right, copy=False) + left, right = left.align(right) return left, right @@ -5939,7 +5934,7 @@ def _binop(self, other: Series, func, level=None, fill_value=None) -> Series: this = self if not self.index.equals(other.index): - this, other = self.align(other, level=level, join="outer", copy=False) + this, other = self.align(other, level=level, join="outer") this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value)