Skip to content

Commit bcc5160

Browse files
authored
CoW: Switch to copy=False everywhere for Series constructor (#52068)
1 parent 0d6787b commit bcc5160

File tree

19 files changed

+34
-29
lines changed

19 files changed

+34
-29
lines changed

pandas/core/algorithms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ def value_counts(
838838
if bins is not None:
839839
from pandas.core.reshape.tile import cut
840840

841-
values = Series(values)
841+
values = Series(values, copy=False)
842842
try:
843843
ii = cut(values, bins, include_lowest=True)
844844
except TypeError as err:
@@ -861,7 +861,7 @@ def value_counts(
861861
else:
862862
if is_extension_array_dtype(values):
863863
# handle Categorical and sparse,
864-
result = Series(values)._values.value_counts(dropna=dropna)
864+
result = Series(values, copy=False)._values.value_counts(dropna=dropna)
865865
result.name = name
866866
result.index.name = index_name
867867
counts = result._values
@@ -893,7 +893,7 @@ def value_counts(
893893
idx = idx.astype(object)
894894
idx.name = index_name
895895

896-
result = Series(counts, index=idx, name=name)
896+
result = Series(counts, index=idx, name=name, copy=False)
897897

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

pandas/core/arrays/_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def value_counts(self, dropna: bool = True) -> Series:
445445

446446
index_arr = self._from_backing_data(np.asarray(result.index._data))
447447
index = Index(index_arr, name=result.index.name)
448-
return Series(result._values, index=index, name=result.name)
448+
return Series(result._values, index=index, name=result.name, copy=False)
449449

450450
def _quantile(
451451
self,

pandas/core/arrays/arrow/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ def value_counts(self, dropna: bool = True) -> Series:
11241124

11251125
index = Index(type(self)(values))
11261126

1127-
return Series(counts, index=index, name="count")
1127+
return Series(counts, index=index, name="count", copy=False)
11281128

11291129
@classmethod
11301130
def _concat_same_type(cls, to_concat) -> Self:

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,9 @@ def value_counts(self, dropna: bool = True) -> Series:
15351535
ix = coerce_indexer_dtype(ix, self.dtype.categories)
15361536
ix = self._from_backing_data(ix)
15371537

1538-
return Series(count, index=CategoricalIndex(ix), dtype="int64", name="count")
1538+
return Series(
1539+
count, index=CategoricalIndex(ix), dtype="int64", name="count", copy=False
1540+
)
15391541

15401542
# error: Argument 2 of "_empty" is incompatible with supertype
15411543
# "NDArrayBackedExtensionArray"; supertype defines the argument type as
@@ -1793,7 +1795,9 @@ def _values_for_rank(self):
17931795
# reorder the categories (so rank can use the float codes)
17941796
# instead of passing an object array to rank
17951797
values = np.array(
1796-
self.rename_categories(Series(self.categories).rank().values)
1798+
self.rename_categories(
1799+
Series(self.categories, copy=False).rank().values
1800+
)
17971801
)
17981802
return values
17991803

pandas/core/arrays/masked.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ def value_counts(self, dropna: bool = True) -> Series:
995995
)
996996

997997
if dropna:
998-
res = Series(value_counts, index=keys, name="count")
998+
res = Series(value_counts, index=keys, name="count", copy=False)
999999
res.index = res.index.astype(self.dtype)
10001000
res = res.astype("Int64")
10011001
return res
@@ -1011,7 +1011,7 @@ def value_counts(self, dropna: bool = True) -> Series:
10111011
mask = np.zeros(len(counts), dtype="bool")
10121012
counts_array = IntegerArray(counts, mask)
10131013

1014-
return Series(counts_array, index=index, name="count")
1014+
return Series(counts_array, index=index, name="count", copy=False)
10151015

10161016
@doc(ExtensionArray.equals)
10171017
def equals(self, other) -> bool:

pandas/core/arrays/sparse/accessor.py

Lines changed: 1 addition & 0 deletions
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

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def value_counts(self, dropna: bool = True) -> Series:
886886
index = Index(keys)
887887
else:
888888
index = keys
889-
return Series(counts, index=index)
889+
return Series(counts, index=index, copy=False)
890890

891891
# --------
892892
# Indexing

pandas/core/arrays/sparse/scipy_sparse.py

Lines changed: 1 addition & 1 deletion
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."

pandas/core/groupby/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ def value_counts(
720720
llab = lambda lab, inc: lab[inc]
721721
else:
722722
# lab is a Categorical with categories an IntervalIndex
723-
cat_ser = cut(Series(val), bins, include_lowest=True)
723+
cat_ser = cut(Series(val, copy=False), bins, include_lowest=True)
724724
cat_obj = cast("Categorical", cat_ser._values)
725725
lev = cat_obj.categories
726726
lab = lev.take(

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def _agg_py_fallback(
15701570

15711571
if values.ndim == 1:
15721572
# For DataFrameGroupBy we only get here with ExtensionArray
1573-
ser = Series(values)
1573+
ser = Series(values, copy=False)
15741574
else:
15751575
# We only get here with values.dtype == object
15761576
# TODO: special case not needed with ArrayManager

pandas/core/indexes/accessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _delegate_property_get(self, name: str): # type: ignore[override]
105105
index = self.orig.index
106106
else:
107107
index = self._parent.index
108-
# return the result as a Series, which is by definition a copy
108+
# return the result as a Series
109109
result = Series(result, index=index, name=self.name).__finalize__(self._parent)
110110

111111
# setting this object will show a SettingWithCopyWarning/Error

pandas/core/reshape/encoding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def _get_dummies_1d(
239239
from pandas.core.reshape.concat import concat
240240

241241
# Series avoids inconsistent NaN handling
242-
codes, levels = factorize_from_iterable(Series(data))
242+
codes, levels = factorize_from_iterable(Series(data, copy=False))
243243

244244
if dtype is None:
245245
dtype = np.dtype(bool)
@@ -313,7 +313,7 @@ def get_empty_frame(data) -> DataFrame:
313313
fill_value=fill_value,
314314
dtype=dtype,
315315
)
316-
sparse_series.append(Series(data=sarr, index=index, name=col))
316+
sparse_series.append(Series(data=sarr, index=index, name=col, copy=False))
317317

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

pandas/core/strings/accessor.py

Lines changed: 2 additions & 2 deletions
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

pandas/core/strings/object_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def _str_get_dummies(self, sep: str = "|"):
377377
arr = sep + arr.astype(str) + sep
378378

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

pandas/core/tools/datetimes.py

Lines changed: 1 addition & 1 deletion
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

pandas/core/window/ewm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def cov_func(x, y):
733733
self.ignore_na,
734734
bias,
735735
)
736-
return Series(result, index=x.index, name=x.name)
736+
return Series(result, index=x.index, name=x.name, copy=False)
737737

738738
return self._apply_pairwise(
739739
self._selected_obj, other, pairwise, cov_func, numeric_only
@@ -810,7 +810,7 @@ def _cov(X, Y):
810810
x_var = _cov(x_array, x_array)
811811
y_var = _cov(y_array, y_array)
812812
result = cov / zsqrt(x_var * y_var)
813-
return Series(result, index=x.index, name=x.name)
813+
return Series(result, index=x.index, name=x.name, copy=False)
814814

815815
return self._apply_pairwise(
816816
self._selected_obj, other, pairwise, cov_func, numeric_only

pandas/core/window/rolling.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def _insert_on_column(self, result: DataFrame, obj: DataFrame) -> None:
384384

385385
if self.on is not None and not self._on.equals(obj.index):
386386
name = self._on.name
387-
extra_col = Series(self._on, index=self.obj.index, name=name)
387+
extra_col = Series(self._on, index=self.obj.index, name=name, copy=False)
388388
if name in result.columns:
389389
# TODO: sure we want to overwrite results?
390390
result[name] = extra_col
@@ -1418,7 +1418,7 @@ def _generate_cython_apply_func(
14181418
def apply_func(values, begin, end, min_periods, raw=raw):
14191419
if not raw:
14201420
# GH 45912
1421-
values = Series(values, index=self._on)
1421+
values = Series(values, index=self._on, copy=False)
14221422
return window_func(values, begin, end, min_periods)
14231423

14241424
return apply_func
@@ -1675,7 +1675,7 @@ def cov_func(x, y):
16751675
notna(x_array + y_array).astype(np.float64), start, end, 0
16761676
)
16771677
result = (mean_x_y - mean_x * mean_y) * (count_x_y / (count_x_y - ddof))
1678-
return Series(result, index=x.index, name=x.name)
1678+
return Series(result, index=x.index, name=x.name, copy=False)
16791679

16801680
return self._apply_pairwise(
16811681
self._selected_obj, other, pairwise, cov_func, numeric_only
@@ -1732,7 +1732,7 @@ def corr_func(x, y):
17321732
)
17331733
denominator = (x_var * y_var) ** 0.5
17341734
result = numerator / denominator
1735-
return Series(result, index=x.index, name=x.name)
1735+
return Series(result, index=x.index, name=x.name, copy=False)
17361736

17371737
return self._apply_pairwise(
17381738
self._selected_obj, other, pairwise, corr_func, numeric_only

pandas/io/stata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ def _do_convert_categoricals(
19891989
# TODO: if we get a non-copying rename_categories, use that
19901990
cat_data = cat_data.rename_categories(categories)
19911991
except ValueError as err:
1992-
vc = Series(categories).value_counts()
1992+
vc = Series(categories, copy=False).value_counts()
19931993
repeated_cats = list(vc.index[vc > 1])
19941994
repeats = "-" * 80 + "\n" + "\n".join(repeated_cats)
19951995
# GH 25772
@@ -2006,7 +2006,7 @@ def _do_convert_categoricals(
20062006
"""
20072007
raise ValueError(msg) from err
20082008
# TODO: is the next line needed above in the data(...) method?
2009-
cat_series = Series(cat_data, index=data.index)
2009+
cat_series = Series(cat_data, index=data.index, copy=False)
20102010
cat_converted_data.append((col, cat_series))
20112011
else:
20122012
cat_converted_data.append((col, data[col]))

pandas/plotting/_matplotlib/boxplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def _grouped_plot_by_column(
289289
ax_values.append(re_plotf)
290290
ax.grid(grid)
291291

292-
result = pd.Series(ax_values, index=columns)
292+
result = pd.Series(ax_values, index=columns, copy=False)
293293

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

0 commit comments

Comments
 (0)