Skip to content

Commit 49e958e

Browse files
phoflpmhatre1
authored andcommitted
CoW: Remove a few copy=False statements (pandas-dev#57346)
1 parent 3998247 commit 49e958e

File tree

14 files changed

+23
-23
lines changed

14 files changed

+23
-23
lines changed

pandas/core/apply.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ def infer_to_same_shape(self, results: ResType, res_index: Index) -> DataFrame:
13611361
result.index = res_index
13621362

13631363
# infer dtypes
1364-
result = result.infer_objects(copy=False)
1364+
result = result.infer_objects()
13651365

13661366
return result
13671367

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

pandas/core/computation/align.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _align_core(terms):
134134
w, category=PerformanceWarning, stacklevel=find_stack_level()
135135
)
136136

137-
obj = ti.reindex(reindexer, axis=axis, copy=False)
137+
obj = ti.reindex(reindexer, axis=axis)
138138
terms[i].update(obj)
139139

140140
terms[i].update(terms[i].value.values)

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ def _wrap_applied_output(
16481648
res_index = self._grouper.result_index
16491649

16501650
result = self.obj._constructor(index=res_index, columns=data.columns)
1651-
result = result.astype(data.dtypes, copy=False)
1651+
result = result.astype(data.dtypes)
16521652
return result
16531653

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

18161816
concat_index = obj.columns
18171817
concatenated = concat(applied, axis=0, verify_integrity=False)
1818-
concatenated = concatenated.reindex(concat_index, axis=1, copy=False)
1818+
concatenated = concatenated.reindex(concat_index, axis=1)
18191819
return self._set_result_index_ordered(concatenated)
18201820

18211821
__examples_dataframe_doc = dedent(

pandas/core/groupby/groupby.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ def _concat_objects(
12411241
indexer, _ = result.index.get_indexer_non_unique(target)
12421242
result = result.take(indexer, axis=0)
12431243
else:
1244-
result = result.reindex(ax, axis=0, copy=False)
1244+
result = result.reindex(ax, axis=0)
12451245

12461246
else:
12471247
result = concat(values, axis=0)
@@ -1269,18 +1269,18 @@ def _set_result_index_ordered(
12691269

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

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

12851285
return result
12861286

@@ -1913,7 +1913,7 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
19131913

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

19181918
if self.obj.ndim == 1:
19191919
# i.e. SeriesGroupBy

pandas/core/indexing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
22802280
has_dtype = hasattr(value, "dtype")
22812281
if isinstance(value, ABCSeries):
22822282
# append a Series
2283-
value = value.reindex(index=self.obj.columns, copy=True)
2283+
value = value.reindex(index=self.obj.columns)
22842284
value.name = indexer
22852285
elif isinstance(value, dict):
22862286
value = Series(
@@ -2310,7 +2310,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
23102310
if not has_dtype:
23112311
# i.e. if we already had a Series or ndarray, keep that
23122312
# dtype. But if we had a list or dict, then do inference
2313-
df = df.infer_objects(copy=False)
2313+
df = df.infer_objects()
23142314
self.obj._mgr = df._mgr
23152315
else:
23162316
self.obj._mgr = self.obj._append(value)._mgr
@@ -2383,7 +2383,8 @@ def ravel(i):
23832383
# we have a frame, with multiple indexers on both axes; and a
23842384
# series, so need to broadcast (see GH5206)
23852385
if sum_aligners == self.ndim and all(is_sequence(_) for _ in indexer):
2386-
ser_values = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values
2386+
# TODO(CoW): copy shouldn't be needed here
2387+
ser_values = ser.reindex(obj.axes[0][indexer[0]]).copy()._values
23872388

23882389
# single indexer
23892390
if len(indexer) > 1 and not multiindex_indexer:

pandas/core/internals/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,11 @@ def _homogenize(
519519
for val in data:
520520
if isinstance(val, (ABCSeries, Index)):
521521
if dtype is not None:
522-
val = val.astype(dtype, copy=False)
522+
val = val.astype(dtype)
523523
if isinstance(val, ABCSeries) and val.index is not index:
524524
# Forces alignment. No need to copy data since we
525525
# are putting it into an ndarray later
526-
val = val.reindex(index, copy=False)
526+
val = val.reindex(index)
527527
refs.append(val._references)
528528
val = val._values
529529
else:

pandas/core/methods/describe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame:
173173

174174
col_names = reorder_columns(ldesc)
175175
d = concat(
176-
[x.reindex(col_names, copy=False) for x in ldesc],
176+
[x.reindex(col_names) for x in ldesc],
177177
axis=1,
178178
sort=False,
179179
)

pandas/core/reshape/encoding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def get_empty_frame(data) -> DataFrame:
339339
)
340340
sparse_series.append(Series(data=sarr, index=index, name=col, copy=False))
341341

342-
return concat(sparse_series, axis=1, copy=False)
342+
return concat(sparse_series, axis=1)
343343

344344
else:
345345
# ensure ndarray layout is column-major

pandas/core/strings/accessor.py

-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@ def cat(
661661
join=(join if join == "inner" else "outer"),
662662
keys=range(len(others)),
663663
sort=False,
664-
copy=False,
665664
)
666665
data, others = data.align(others, join=join)
667666
others = [others[x] for x in others] # again list of Series

pandas/core/window/ewm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ def mean(self, *args, update=None, update_times=None, **kwargs):
10721072
result_kwargs["columns"] = self._selected_obj.columns
10731073
else:
10741074
result_kwargs["name"] = self._selected_obj.name
1075-
np_array = self._selected_obj.astype(np.float64, copy=False).to_numpy()
1075+
np_array = self._selected_obj.astype(np.float64).to_numpy()
10761076
ewma_func = generate_online_numba_ewma_func(
10771077
**get_jit_arguments(self.engine_kwargs)
10781078
)

pandas/core/window/rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _create_data(self, obj: NDFrameT, numeric_only: bool = False) -> NDFrameT:
270270
"""
271271
# filter out the on from the object
272272
if self.on is not None and not isinstance(self.on, Index) and obj.ndim == 2:
273-
obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False)
273+
obj = obj.reindex(columns=obj.columns.difference([self.on]))
274274
if obj.ndim > 1 and numeric_only:
275275
obj = self._make_numeric_only(obj)
276276
return obj

pandas/io/parsers/base_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ def _isindex(colspec):
13541354
date_cols.update(old_names)
13551355

13561356
if isinstance(data_dict, DataFrame):
1357-
data_dict = concat([DataFrame(new_data), data_dict], axis=1, copy=False)
1357+
data_dict = concat([DataFrame(new_data), data_dict], axis=1)
13581358
else:
13591359
data_dict.update(new_data)
13601360
new_cols.extend(columns)

pandas/plotting/_matplotlib/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def _compute_plot_data(self) -> None:
678678

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

684684
# GH23719, allow plotting boolean

pandas/plotting/_matplotlib/hist.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _adjust_bins(self, bins: int | np.ndarray | list[np.ndarray]):
9494

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

0 commit comments

Comments
 (0)