Skip to content

Commit 5d69654

Browse files
Backport PR #52068 on branch 2.0.x (CoW: Switch to copy=False everywhere for Series constructor) (#52367)
Backport PR #52068: CoW: Switch to copy=False everywhere for Series constructor Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 9bf45db commit 5d69654

File tree

19 files changed

+34
-29
lines changed

19 files changed

+34
-29
lines changed

Diff for: pandas/core/algorithms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def value_counts(
837837
if bins is not None:
838838
from pandas.core.reshape.tile import cut
839839

840-
values = Series(values)
840+
values = Series(values, copy=False)
841841
try:
842842
ii = cut(values, bins, include_lowest=True)
843843
except TypeError as err:
@@ -860,7 +860,7 @@ def value_counts(
860860
else:
861861
if is_extension_array_dtype(values):
862862
# handle Categorical and sparse,
863-
result = Series(values)._values.value_counts(dropna=dropna)
863+
result = Series(values, copy=False)._values.value_counts(dropna=dropna)
864864
result.name = name
865865
result.index.name = index_name
866866
counts = result._values
@@ -892,7 +892,7 @@ def value_counts(
892892
idx = idx.astype(object)
893893
idx.name = index_name
894894

895-
result = Series(counts, index=idx, name=name)
895+
result = Series(counts, index=idx, name=name, copy=False)
896896

897897
if sort:
898898
result = result.sort_values(ascending=ascending)

Diff for: pandas/core/arrays/_mixins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def value_counts(self, dropna: bool = True) -> Series:
448448

449449
index_arr = self._from_backing_data(np.asarray(result.index._data))
450450
index = Index(index_arr, name=result.index.name)
451-
return Series(result._values, index=index, name=result.name)
451+
return Series(result._values, index=index, name=result.name, copy=False)
452452

453453
def _quantile(
454454
self: NDArrayBackedExtensionArrayT,

Diff for: pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ def value_counts(self, dropna: bool = True) -> Series:
11041104

11051105
index = Index(type(self)(values))
11061106

1107-
return Series(counts, index=index, name="count")
1107+
return Series(counts, index=index, name="count", copy=False)
11081108

11091109
@classmethod
11101110
def _concat_same_type(

Diff for: pandas/core/arrays/categorical.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,9 @@ def value_counts(self, dropna: bool = True) -> Series:
15001500
ix = coerce_indexer_dtype(ix, self.dtype.categories)
15011501
ix = self._from_backing_data(ix)
15021502

1503-
return Series(count, index=CategoricalIndex(ix), dtype="int64", name="count")
1503+
return Series(
1504+
count, index=CategoricalIndex(ix), dtype="int64", name="count", copy=False
1505+
)
15041506

15051507
# error: Argument 2 of "_empty" is incompatible with supertype
15061508
# "NDArrayBackedExtensionArray"; supertype defines the argument type as
@@ -1758,7 +1760,9 @@ def _values_for_rank(self):
17581760
# reorder the categories (so rank can use the float codes)
17591761
# instead of passing an object array to rank
17601762
values = np.array(
1761-
self.rename_categories(Series(self.categories).rank().values)
1763+
self.rename_categories(
1764+
Series(self.categories, copy=False).rank().values
1765+
)
17621766
)
17631767
return values
17641768

Diff for: pandas/core/arrays/masked.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ def value_counts(self, dropna: bool = True) -> Series:
10021002
)
10031003

10041004
if dropna:
1005-
res = Series(value_counts, index=keys, name="count")
1005+
res = Series(value_counts, index=keys, name="count", copy=False)
10061006
res.index = res.index.astype(self.dtype)
10071007
res = res.astype("Int64")
10081008
return res
@@ -1018,7 +1018,7 @@ def value_counts(self, dropna: bool = True) -> Series:
10181018
mask = np.zeros(len(counts), dtype="bool")
10191019
counts_array = IntegerArray(counts, mask)
10201020

1021-
return Series(counts_array, index=index, name="count")
1021+
return Series(counts_array, index=index, name="count", copy=False)
10221022

10231023
@doc(ExtensionArray.equals)
10241024
def equals(self, other) -> bool:

Diff for: pandas/core/arrays/sparse/accessor.py

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def to_dense(self) -> Series:
219219
self._parent.array.to_dense(),
220220
index=self._parent.index,
221221
name=self._parent.name,
222+
copy=False,
222223
)
223224

224225

Diff for: pandas/core/arrays/sparse/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def value_counts(self, dropna: bool = True) -> Series:
890890
index = Index(keys)
891891
else:
892892
index = keys
893-
return Series(counts, index=index)
893+
return Series(counts, index=index, copy=False)
894894

895895
# --------
896896
# Indexing

Diff for: pandas/core/arrays/sparse/scipy_sparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def coo_to_sparse_series(
195195
from pandas import SparseDtype
196196

197197
try:
198-
ser = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
198+
ser = Series(A.data, MultiIndex.from_arrays((A.row, A.col)), copy=False)
199199
except AttributeError as err:
200200
raise TypeError(
201201
f"Expected coo_matrix. Got {type(A).__name__} instead."

Diff for: pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def value_counts(
691691
llab = lambda lab, inc: lab[inc]
692692
else:
693693
# lab is a Categorical with categories an IntervalIndex
694-
cat_ser = cut(Series(val), bins, include_lowest=True)
694+
cat_ser = cut(Series(val, copy=False), bins, include_lowest=True)
695695
cat_obj = cast("Categorical", cat_ser._values)
696696
lev = cat_obj.categories
697697
lab = lev.take(

Diff for: pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ def _agg_py_fallback(
14391439

14401440
if values.ndim == 1:
14411441
# For DataFrameGroupBy we only get here with ExtensionArray
1442-
ser = Series(values)
1442+
ser = Series(values, copy=False)
14431443
else:
14441444
# We only get here with values.dtype == object
14451445
# TODO: special case not needed with ArrayManager

Diff for: pandas/core/indexes/accessors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def _delegate_property_get(self, name):
101101
index = self.orig.index
102102
else:
103103
index = self._parent.index
104-
# return the result as a Series, which is by definition a copy
104+
# return the result as a Series
105105
result = Series(result, index=index, name=self.name).__finalize__(self._parent)
106106

107107
# setting this object will show a SettingWithCopyWarning/Error

Diff for: pandas/core/reshape/encoding.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def _get_dummies_1d(
236236
from pandas.core.reshape.concat import concat
237237

238238
# Series avoids inconsistent NaN handling
239-
codes, levels = factorize_from_iterable(Series(data))
239+
codes, levels = factorize_from_iterable(Series(data, copy=False))
240240

241241
if dtype is None:
242242
dtype = np.dtype(bool)
@@ -310,7 +310,7 @@ def get_empty_frame(data) -> DataFrame:
310310
fill_value=fill_value,
311311
dtype=dtype,
312312
)
313-
sparse_series.append(Series(data=sarr, index=index, name=col))
313+
sparse_series.append(Series(data=sarr, index=index, name=col, copy=False))
314314

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

Diff for: pandas/core/strings/accessor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def _get_series_list(self, others):
379379
if isinstance(others, ABCSeries):
380380
return [others]
381381
elif isinstance(others, ABCIndex):
382-
return [Series(others._values, index=idx, dtype=others.dtype)]
382+
return [Series(others, index=idx, dtype=others.dtype)]
383383
elif isinstance(others, ABCDataFrame):
384384
return [others[x] for x in others]
385385
elif isinstance(others, np.ndarray) and others.ndim == 2:
@@ -634,7 +634,7 @@ def cat(
634634
else:
635635
dtype = self._orig.dtype
636636
res_ser = Series(
637-
result, dtype=dtype, index=data.index, name=self._orig.name
637+
result, dtype=dtype, index=data.index, name=self._orig.name, copy=False
638638
)
639639
out = res_ser.__finalize__(self._orig, method="str_cat")
640640
return out

Diff for: pandas/core/strings/object_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def _str_get_dummies(self, sep: str = "|"):
376376
arr = sep + arr.astype(str) + sep
377377

378378
tags: set[str] = set()
379-
for ts in Series(arr).str.split(sep):
379+
for ts in Series(arr, copy=False).str.split(sep):
380380
tags.update(ts)
381381
tags2 = sorted(tags - {""})
382382

Diff for: pandas/core/tools/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def _maybe_cache(
250250
cache_dates = convert_listlike(unique_dates, format)
251251
# GH#45319
252252
try:
253-
cache_array = Series(cache_dates, index=unique_dates)
253+
cache_array = Series(cache_dates, index=unique_dates, copy=False)
254254
except OutOfBoundsDatetime:
255255
return cache_array
256256
# GH#39882 and GH#35888 in case of None and NaT we get duplicates

Diff for: pandas/core/window/ewm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def cov_func(x, y):
730730
self.ignore_na,
731731
bias,
732732
)
733-
return Series(result, index=x.index, name=x.name)
733+
return Series(result, index=x.index, name=x.name, copy=False)
734734

735735
return self._apply_pairwise(
736736
self._selected_obj, other, pairwise, cov_func, numeric_only
@@ -807,7 +807,7 @@ def _cov(X, Y):
807807
x_var = _cov(x_array, x_array)
808808
y_var = _cov(y_array, y_array)
809809
result = cov / zsqrt(x_var * y_var)
810-
return Series(result, index=x.index, name=x.name)
810+
return Series(result, index=x.index, name=x.name, copy=False)
811811

812812
return self._apply_pairwise(
813813
self._selected_obj, other, pairwise, cov_func, numeric_only

Diff for: pandas/core/window/rolling.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def _insert_on_column(self, result: DataFrame, obj: DataFrame) -> None:
383383

384384
if self.on is not None and not self._on.equals(obj.index):
385385
name = self._on.name
386-
extra_col = Series(self._on, index=self.obj.index, name=name)
386+
extra_col = Series(self._on, index=self.obj.index, name=name, copy=False)
387387
if name in result.columns:
388388
# TODO: sure we want to overwrite results?
389389
result[name] = extra_col
@@ -1413,7 +1413,7 @@ def _generate_cython_apply_func(
14131413
def apply_func(values, begin, end, min_periods, raw=raw):
14141414
if not raw:
14151415
# GH 45912
1416-
values = Series(values, index=self._on)
1416+
values = Series(values, index=self._on, copy=False)
14171417
return window_func(values, begin, end, min_periods)
14181418

14191419
return apply_func
@@ -1670,7 +1670,7 @@ def cov_func(x, y):
16701670
notna(x_array + y_array).astype(np.float64), start, end, 0
16711671
)
16721672
result = (mean_x_y - mean_x * mean_y) * (count_x_y / (count_x_y - ddof))
1673-
return Series(result, index=x.index, name=x.name)
1673+
return Series(result, index=x.index, name=x.name, copy=False)
16741674

16751675
return self._apply_pairwise(
16761676
self._selected_obj, other, pairwise, cov_func, numeric_only
@@ -1727,7 +1727,7 @@ def corr_func(x, y):
17271727
)
17281728
denominator = (x_var * y_var) ** 0.5
17291729
result = numerator / denominator
1730-
return Series(result, index=x.index, name=x.name)
1730+
return Series(result, index=x.index, name=x.name, copy=False)
17311731

17321732
return self._apply_pairwise(
17331733
self._selected_obj, other, pairwise, corr_func, numeric_only

Diff for: pandas/io/stata.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ def _do_convert_categoricals(
19881988
# TODO: if we get a non-copying rename_categories, use that
19891989
cat_data = cat_data.rename_categories(categories)
19901990
except ValueError as err:
1991-
vc = Series(categories).value_counts()
1991+
vc = Series(categories, copy=False).value_counts()
19921992
repeated_cats = list(vc.index[vc > 1])
19931993
repeats = "-" * 80 + "\n" + "\n".join(repeated_cats)
19941994
# GH 25772
@@ -2005,7 +2005,7 @@ def _do_convert_categoricals(
20052005
"""
20062006
raise ValueError(msg) from err
20072007
# TODO: is the next line needed above in the data(...) method?
2008-
cat_series = Series(cat_data, index=data.index)
2008+
cat_series = Series(cat_data, index=data.index, copy=False)
20092009
cat_converted_data.append((col, cat_series))
20102010
else:
20112011
cat_converted_data.append((col, data[col]))

Diff for: pandas/plotting/_matplotlib/boxplot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def _grouped_plot_by_column(
288288
ax_values.append(re_plotf)
289289
ax.grid(grid)
290290

291-
result = pd.Series(ax_values, index=columns)
291+
result = pd.Series(ax_values, index=columns, copy=False)
292292

293293
# Return axes in multiplot case, maybe revisit later # 985
294294
if return_type is None:

0 commit comments

Comments
 (0)