-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
PERF: Pass copy=False to Index constructor #63451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,6 +300,7 @@ def _new_Index(cls, d): | |
| """ | ||
| # required for backward compat, because PI can't be instantiated with | ||
| # ordinals through __new__ GH #13277 | ||
| d["copy"] = False | ||
| if issubclass(cls, ABCPeriodIndex): | ||
| from pandas.core.indexes.period import _new_PeriodIndex | ||
|
|
||
|
|
@@ -692,7 +693,7 @@ def _with_infer(cls, *args, **kwargs): | |
| # "ndarray[Any, Any]" | ||
| values = lib.maybe_convert_objects(result._values) # type: ignore[arg-type] | ||
| if values.dtype.kind in "iufb": | ||
| return Index(values, name=result.name) | ||
| return Index(values, name=result.name, copy=False) | ||
|
|
||
| return result | ||
|
|
||
|
|
@@ -2775,7 +2776,7 @@ def fillna(self, value): | |
| # no need to care metadata other than name | ||
| # because it can't have freq if it has NaTs | ||
| # _with_infer needed for test_fillna_categorical | ||
| return Index._with_infer(result, name=self.name) | ||
| return Index._with_infer(result, name=self.name, copy=False) | ||
| return self._view() | ||
|
|
||
| def dropna(self, how: AnyAll = "any") -> Self: | ||
|
|
@@ -3910,8 +3911,8 @@ def _get_fill_indexer( | |
| if not (self.is_monotonic_increasing or self.is_monotonic_decreasing): | ||
| raise ValueError("index must be monotonic increasing or decreasing") | ||
| encoded = self.append(target)._engine.values # type: ignore[union-attr] | ||
| self_encoded = Index(encoded[: len(self)]) | ||
| target_encoded = Index(encoded[len(self) :]) | ||
| self_encoded = Index(encoded[: len(self)], copy=False) | ||
| target_encoded = Index(encoded[len(self) :], copy=False) | ||
| return self_encoded._get_fill_indexer( | ||
| target_encoded, method, limit, tolerance | ||
| ) | ||
|
|
@@ -4338,7 +4339,7 @@ def _reindex_non_unique( | |
| new_indexer[~check] = -1 | ||
|
|
||
| if not isinstance(self, ABCMultiIndex): | ||
| new_index = Index(new_labels, name=self.name) | ||
| new_index = Index(new_labels, name=self.name, copy=False) | ||
| else: | ||
| new_index = type(self).from_tuples(new_labels, names=self.names) | ||
| return new_index, indexer, new_indexer | ||
|
|
@@ -4487,7 +4488,7 @@ def join( | |
| and not self.categories.equals(other.categories) | ||
| ): | ||
| # dtypes are "equal" but categories are in different order | ||
| other = Index(other._values.reorder_categories(self.categories)) | ||
| other = Index(other._values.reorder_categories(self.categories), copy=False) | ||
|
|
||
| _validate_join_method(how) | ||
|
|
||
|
|
@@ -4930,7 +4931,9 @@ def _wrap_join_result( | |
| elif ridx is None: | ||
| join_index = other | ||
| else: | ||
| join_index = self._constructor._with_infer(joined, dtype=self.dtype) | ||
| join_index = self._constructor._with_infer( | ||
| joined, dtype=self.dtype, copy=False | ||
| ) | ||
|
|
||
| names = other.names if how == "right" else self.names | ||
| if join_index.names != names: | ||
|
|
@@ -6368,7 +6371,7 @@ def _maybe_downcast_for_indexing(self, other: Index) -> tuple[Index, Index]: | |
| other = type(self).from_tuples(other) # type: ignore[attr-defined] | ||
| except (TypeError, ValueError): | ||
| # let's instead try with a straight Index | ||
| self = Index(self._values) | ||
| self = Index(self._values, copy=False) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but also any place we're passing in |
||
|
|
||
| if not is_object_dtype(self.dtype) and is_object_dtype(other.dtype): | ||
| # Reverse op so we dont need to re-implement on the subclasses | ||
|
|
@@ -7124,7 +7127,7 @@ def insert(self, loc: int, item) -> Index: | |
| new_values[loc] = item | ||
|
|
||
| # GH#51363 stopped doing dtype inference here | ||
| out = Index(new_values, dtype=new_values.dtype, name=self.name) | ||
| out = Index(new_values, dtype=new_values.dtype, name=self.name, copy=False) | ||
| return out | ||
|
|
||
| def drop( | ||
|
|
@@ -7220,7 +7223,7 @@ def infer_objects(self, copy: bool = True) -> Index: | |
| ) | ||
| if copy and res_values is values: | ||
| return self.copy() | ||
| result = Index(res_values, name=self.name) | ||
| result = Index(res_values, name=self.name, copy=False) | ||
| if not copy and res_values is values and self._references is not None: | ||
| result._references = self._references | ||
| result._references.add_index_reference(result) | ||
|
|
@@ -7329,10 +7332,10 @@ def _logical_method(self, other, op): | |
| def _construct_result(self, result, name, other): | ||
| if isinstance(result, tuple): | ||
| return ( | ||
| Index(result[0], name=name, dtype=result[0].dtype), | ||
| Index(result[1], name=name, dtype=result[1].dtype), | ||
| Index(result[0], name=name, dtype=result[0].dtype, copy=False), | ||
| Index(result[1], name=name, dtype=result[1].dtype, copy=False), | ||
| ) | ||
| return Index(result, name=name, dtype=result.dtype) | ||
| return Index(result, name=name, dtype=result.dtype, copy=False) | ||
|
|
||
| def _arith_method(self, other, op): | ||
| if ( | ||
|
|
@@ -7350,7 +7353,7 @@ def _arith_method(self, other, op): | |
| @final | ||
| def _unary_method(self, op): | ||
| result = op(self._values) | ||
| return Index(result, name=self.name) | ||
| return Index(result, name=self.name, copy=False) | ||
|
|
||
| def __abs__(self) -> Index: | ||
| return self._unary_method(operator.abs) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, this one is not needed I think because
resultreturned from putmask above is an Index, and so we already do a shallow copy by default.But no harm in keeping it to avoid confusion ;)