Skip to content

CoW: Remove a few copy=False statements #57346

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 1 commit 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
4 changes: 2 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ def infer_to_same_shape(self, results: ResType, res_index: Index) -> DataFrame:
result.index = res_index

# infer dtypes
result = result.infer_objects(copy=False)
result = result.infer_objects()

return result

Expand Down Expand Up @@ -1873,7 +1873,7 @@ def relabel_result(
# assign the new user-provided "named aggregation" as index names, and reindex
# it based on the whole user-provided names.
s.index = reordered_indexes[idx : idx + len(fun)]
reordered_result_in_dict[col] = s.reindex(columns, copy=False)
reordered_result_in_dict[col] = s.reindex(columns)
idx = idx + len(fun)
return reordered_result_in_dict

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _align_core(terms):
w, category=PerformanceWarning, stacklevel=find_stack_level()
)

obj = ti.reindex(reindexer, axis=axis, copy=False)
obj = ti.reindex(reindexer, axis=axis)
terms[i].update(obj)

terms[i].update(terms[i].value.values)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ def _wrap_applied_output(
res_index = self._grouper.result_index

result = self.obj._constructor(index=res_index, columns=data.columns)
result = result.astype(data.dtypes, copy=False)
result = result.astype(data.dtypes)
return result

# GH12824
Expand Down Expand Up @@ -1815,7 +1815,7 @@ def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):

concat_index = obj.columns
concatenated = concat(applied, axis=0, verify_integrity=False)
concatenated = concatenated.reindex(concat_index, axis=1, copy=False)
concatenated = concatenated.reindex(concat_index, axis=1)
return self._set_result_index_ordered(concatenated)

__examples_dataframe_doc = dedent(
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ def _concat_objects(
indexer, _ = result.index.get_indexer_non_unique(target)
result = result.take(indexer, axis=0)
else:
result = result.reindex(ax, axis=0, copy=False)
result = result.reindex(ax, axis=0)

else:
result = concat(values, axis=0)
Expand Down Expand Up @@ -1269,18 +1269,18 @@ def _set_result_index_ordered(

if self._grouper.is_monotonic and not self._grouper.has_dropped_na:
# shortcut if we have an already ordered grouper
result = result.set_axis(index, axis=0, copy=False)
result = result.set_axis(index, axis=0)
return result

# row order is scrambled => sort the rows by position in original index
original_positions = Index(self._grouper.result_ilocs)
result = result.set_axis(original_positions, axis=0, copy=False)
result = result.set_axis(original_positions, axis=0)
result = result.sort_index(axis=0)
if self._grouper.has_dropped_na:
# Add back in any missing rows due to dropna - index here is integral
# with values referring to the row of the input so can use RangeIndex
result = result.reindex(RangeIndex(len(index)), axis=0)
result = result.set_axis(index, axis=0, copy=False)
result = result.set_axis(index, axis=0)

return result

Expand Down Expand Up @@ -1913,7 +1913,7 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:

# for each col, reshape to size of original frame by take operation
ids = self._grouper.ids
result = result.reindex(self._grouper.result_index, axis=0, copy=False)
result = result.reindex(self._grouper.result_index, axis=0)

if self.obj.ndim == 1:
# i.e. SeriesGroupBy
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
has_dtype = hasattr(value, "dtype")
if isinstance(value, ABCSeries):
# append a Series
value = value.reindex(index=self.obj.columns, copy=True)
value = value.reindex(index=self.obj.columns)
value.name = indexer
elif isinstance(value, dict):
value = Series(
Expand Down Expand Up @@ -2310,7 +2310,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
if not has_dtype:
# i.e. if we already had a Series or ndarray, keep that
# dtype. But if we had a list or dict, then do inference
df = df.infer_objects(copy=False)
df = df.infer_objects()
self.obj._mgr = df._mgr
else:
self.obj._mgr = self.obj._append(value)._mgr
Expand Down Expand Up @@ -2383,7 +2383,8 @@ 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):
ser_values = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values
# TODO(CoW): copy shouldn't be needed here
ser_values = ser.reindex(obj.axes[0][indexer[0]]).copy()._values

# single indexer
if len(indexer) > 1 and not multiindex_indexer:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,11 @@ def _homogenize(
for val in data:
if isinstance(val, (ABCSeries, Index)):
if dtype is not None:
val = val.astype(dtype, copy=False)
val = val.astype(dtype)
if isinstance(val, ABCSeries) and val.index is not index:
# Forces alignment. No need to copy data since we
# are putting it into an ndarray later
val = val.reindex(index, copy=False)
val = val.reindex(index)
refs.append(val._references)
val = val._values
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/methods/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame:

col_names = reorder_columns(ldesc)
d = concat(
[x.reindex(col_names, copy=False) for x in ldesc],
[x.reindex(col_names) for x in ldesc],
axis=1,
sort=False,
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def get_empty_frame(data) -> DataFrame:
)
sparse_series.append(Series(data=sarr, index=index, name=col, copy=False))

return concat(sparse_series, axis=1, copy=False)
return concat(sparse_series, axis=1)

else:
# ensure ndarray layout is column-major
Expand Down
1 change: 0 additions & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ def cat(
join=(join if join == "inner" else "outer"),
keys=range(len(others)),
sort=False,
copy=False,
)
data, others = data.align(others, join=join)
others = [others[x] for x in others] # again list of Series
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def mean(self, *args, update=None, update_times=None, **kwargs):
result_kwargs["columns"] = self._selected_obj.columns
else:
result_kwargs["name"] = self._selected_obj.name
np_array = self._selected_obj.astype(np.float64, copy=False).to_numpy()
np_array = self._selected_obj.astype(np.float64).to_numpy()
ewma_func = generate_online_numba_ewma_func(
**get_jit_arguments(self.engine_kwargs)
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _create_data(self, obj: NDFrameT, numeric_only: bool = False) -> NDFrameT:
"""
# filter out the on from the object
if self.on is not None and not isinstance(self.on, Index) and obj.ndim == 2:
obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False)
obj = obj.reindex(columns=obj.columns.difference([self.on]))
if obj.ndim > 1 and numeric_only:
obj = self._make_numeric_only(obj)
return obj
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ def _isindex(colspec):
date_cols.update(old_names)

if isinstance(data_dict, DataFrame):
data_dict = concat([DataFrame(new_data), data_dict], axis=1, copy=False)
data_dict = concat([DataFrame(new_data), data_dict], axis=1)
else:
data_dict.update(new_data)
new_cols.extend(columns)
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def _compute_plot_data(self) -> None:

# GH16953, infer_objects is needed as fallback, for ``Series``
# with ``dtype == object``
data = data.infer_objects(copy=False)
data = data.infer_objects()
include_type = [np.number, "datetime", "datetimetz", "timedelta"]

# GH23719, allow plotting boolean
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _adjust_bins(self, bins: int | np.ndarray | list[np.ndarray]):

def _calculate_bins(self, data: Series | DataFrame, bins) -> np.ndarray:
"""Calculate bins given data"""
nd_values = data.infer_objects(copy=False)._get_numeric_data()
nd_values = data.infer_objects()._get_numeric_data()
values = np.ravel(nd_values)
values = values[~isna(values)]

Expand Down