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

pandas/core/algorithms.py

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

pandas/core/arrays/_mixins.py

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

pandas/core/arrays/arrow/array.py

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

pandas/core/arrays/categorical.py

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

pandas/core/arrays/masked.py

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

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
@@ -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

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
@@ -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(

pandas/core/groupby/groupby.py

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

0 commit comments

Comments
 (0)