Skip to content

Commit e9ec9a8

Browse files
PERF: Pass copy=False to Index constructor (#63451)
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
1 parent e3d49b7 commit e9ec9a8

34 files changed

+94
-78
lines changed

pandas/_testing/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def box_expected(expected, box_cls, transpose: bool = True):
290290
else:
291291
expected = pd.array(expected, copy=False)
292292
elif box_cls is Index:
293-
expected = Index(expected)
293+
expected = Index(expected, copy=False)
294294
elif box_cls is Series:
295295
expected = Series(expected)
296296
elif box_cls is DataFrame:

pandas/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def index_with_missing(request):
769769
vals = ind.values.copy()
770770
vals[0] = None
771771
vals[-1] = None
772-
return type(ind)(vals)
772+
return type(ind)(vals, copy=False)
773773

774774

775775
# ----------------------------------------------------------------

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def value_counts_internal(
926926

927927
# Starting in 3.0, we no longer perform dtype inference on the
928928
# Index object we construct here, xref GH#56161
929-
idx = Index(keys, dtype=keys.dtype, name=index_name)
929+
idx = Index(keys, dtype=keys.dtype, name=index_name, copy=False)
930930

931931
if (
932932
not sort

pandas/core/apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ def relabel_result(
19721972
fun = [
19731973
com.get_callable_name(f) if not isinstance(f, str) else f for f in fun
19741974
]
1975-
col_idx_order = Index(s.index).get_indexer(fun)
1975+
col_idx_order = Index(s.index, copy=False).get_indexer(fun)
19761976
valid_idx = col_idx_order != -1
19771977
if valid_idx.any():
19781978
s = s.iloc[col_idx_order[valid_idx]]

pandas/core/arrays/_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def value_counts(self, dropna: bool = True) -> Series:
496496
result = value_counts(values, sort=False, dropna=dropna)
497497

498498
index_arr = self._from_backing_data(np.asarray(result.index._data))
499-
index = Index(index_arr, name=result.index.name)
499+
index = Index(index_arr, name=result.index.name, copy=False)
500500
return Series(result._values, index=index, name=result.name, copy=False)
501501

502502
def _quantile(

pandas/core/arrays/arrow/array.py

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

18051805
counts = ArrowExtensionArray(counts)
18061806

1807-
index = Index(self._from_pyarrow_array(values))
1807+
index = Index(self._from_pyarrow_array(values), copy=False)
18081808

18091809
return Series(counts, index=index, name="count", copy=False)
18101810

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _from_inferred_categories(
670670
to_timedelta,
671671
)
672672

673-
cats = Index(inferred_categories)
673+
cats = Index(inferred_categories, copy=False)
674674
known_categories = (
675675
isinstance(dtype, CategoricalDtype) and dtype.categories is not None
676676
)
@@ -2397,7 +2397,7 @@ def _validate_listlike(self, value):
23972397
from pandas import Index
23982398

23992399
# tupleize_cols=False for e.g. test_fillna_iterable_category GH#41914
2400-
to_add = Index._with_infer(value, tupleize_cols=False).difference(
2400+
to_add = Index._with_infer(value, tupleize_cols=False, copy=False).difference(
24012401
self.categories
24022402
)
24032403

pandas/core/arrays/masked.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,8 @@ def value_counts(self, dropna: bool = True) -> Series:
14221422
self.dtype.construct_array_type()(
14231423
keys, # type: ignore[arg-type]
14241424
mask_index,
1425-
)
1425+
),
1426+
copy=False,
14261427
)
14271428
return Series(arr, index=index, name="count", copy=False)
14281429

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ def value_counts(self, dropna: bool = True) -> Series:
961961
counts = np.insert(counts, 0, fcounts)
962962

963963
if not isinstance(keys, ABCIndex):
964-
index = Index(keys)
964+
index = Index(keys, copy=False)
965965
else:
966966
index = keys
967967
return Series(counts, index=index, copy=False)

pandas/core/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,11 +1312,11 @@ def factorize(
13121312
from pandas import Index
13131313

13141314
try:
1315-
uniques = Index(uniques, dtype=self.dtype)
1315+
uniques = Index(uniques, dtype=self.dtype, copy=False)
13161316
except NotImplementedError:
13171317
# not all dtypes are supported in Index that are allowed for Series
13181318
# e.g. float16 or bytes
1319-
uniques = Index(uniques)
1319+
uniques = Index(uniques, copy=False)
13201320
return codes, uniques
13211321

13221322
_shared_docs["searchsorted"] = """

0 commit comments

Comments
 (0)