Skip to content

Commit

Permalink
PERF: _shallow_copy copies cache
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Mar 11, 2020
1 parent 8ac101d commit a3d5f1f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
6 changes: 3 additions & 3 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ Performance improvements
- Performance improvement in :class:`Timedelta` constructor (:issue:`30543`)
- Performance improvement in :class:`Timestamp` constructor (:issue:`30543`)
- Performance improvement in flex arithmetic ops between :class:`DataFrame` and :class:`Series` with ``axis=0`` (:issue:`31296`)
- The internal :meth:`Index._shallow_copy` now copies cached attributes over to the new index,
avoiding creating these again on the new index. This can speed up many operations
that depend on creating copies of existing indexes (:issue:`28584`)
- The internal index method :meth:`~Index._shallow_copy` index classes now copies cached attributes over to the new index,
avoiding creating these again on the new index. This can speed up many operations that depend on creating copies of
existing indexes (:issue:`28584`)

.. ---------------------------------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def _simple_new(cls, values: Categorical, name: Label = None):

result._data = values
result.name = name
result._cache = {}

result._reset_identity()
result._no_setting_name = False
Expand All @@ -242,14 +243,9 @@ def _simple_new(cls, values: Categorical, name: Label = None):

@Appender(Index._shallow_copy.__doc__)
def _shallow_copy(self, values=None, name: Label = no_default):
name = self.name if name is no_default else name

if values is None:
values = self.values

cat = Categorical(values, dtype=self.dtype)

return type(self)._simple_new(cat, name=name)
if values is not None:
values = Categorical(values, dtype=self.dtype)
return super()._shallow_copy(values=values, name=name)

def _is_dtype_compat(self, other) -> bool:
"""
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ def _set_freq(self, freq):

def _shallow_copy(self, values=None, name: Label = lib.no_default):
name = self.name if name is lib.no_default else name
cache = self._cache if values is None else {}

if values is None:
values = self._data
Expand All @@ -636,7 +637,9 @@ def _shallow_copy(self, values=None, name: Label = lib.no_default):
del attributes["freq"]

attributes["name"] = name
return type(self)._simple_new(values, **attributes)
result = self._simple_new(values, **attributes)
result._cache = cache
return result

# --------------------------------------------------------------------
# Set Operation Methods
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
result = object.__new__(cls)
result._data = dtarr
result.name = name
result._cache = {}
result._no_setting_name = False
# For groupby perf. See note in indexes/base about _index_data
result._index_data = dtarr._data
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def _simple_new(cls, array: IntervalArray, name: Label = None):
result = IntervalMixin.__new__(cls)
result._data = array
result.name = name
result._cache = {}
result._no_setting_name = False
result._reset_identity()
return result
Expand Down Expand Up @@ -332,12 +333,15 @@ def from_tuples(
# --------------------------------------------------------------------

@Appender(Index._shallow_copy.__doc__)
def _shallow_copy(self, values=None, **kwargs):
def _shallow_copy(self, values=None, name: Label = lib.no_default):
name = self.name if name is lib.no_default else name
cache = self._cache if values is None else {}
if values is None:
values = self._data
attributes = self._get_attributes_dict()
attributes.update(kwargs)
return self._simple_new(values, **attributes)

result = self._simple_new(values, name=name)
result._cache = cache
return result

@cache_readonly
def _isnan(self):
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def _simple_new(cls, values: PeriodArray, name: Label = None):
# For groupby perf. See note in indexes/base about _index_data
result._index_data = values._data
result.name = name
result._cache = {}
result._reset_identity()
return result

Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
result = object.__new__(cls)
result._data = values
result._name = name
result._cache = {}
# For groupby perf. See note in indexes/base about _index_data
result._index_data = values._data

Expand Down

0 comments on commit a3d5f1f

Please sign in to comment.