From 8df40ffdea13f0d28a192f818f37fc2647a52d0d Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 5 Oct 2021 16:07:27 -0700 Subject: [PATCH] REF: remove _get_attributes_dict --- pandas/core/indexes/base.py | 30 ++++++++++-------------------- pandas/core/indexes/category.py | 2 -- pandas/core/indexes/datetimes.py | 7 +------ pandas/core/indexes/interval.py | 8 ++++++-- pandas/core/indexes/period.py | 1 - pandas/core/indexes/range.py | 5 ++--- 6 files changed, 19 insertions(+), 34 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c9e128ffc4289..2b49a88e27961 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -728,13 +728,6 @@ def _format_duplicate_message(self) -> DataFrame: # -------------------------------------------------------------------- # Index Internals Methods - @final - def _get_attributes_dict(self) -> dict[str_t, Any]: - """ - Return an attributes dict for my class. - """ - return {k: getattr(self, k, None) for k in self._attributes} - def _shallow_copy(self: _IndexT, values, name: Hashable = no_default) -> _IndexT: """ Create a new Index with the same class as the caller, don't copy the @@ -859,9 +852,7 @@ def __array_wrap__(self, result, context=None): if is_bool_dtype(result) or lib.is_scalar(result) or np.ndim(result) > 1: return result - attrs = self._get_attributes_dict() - attrs.pop("freq", None) # For DatetimeIndex/TimedeltaIndex - return Index(result, **attrs) + return Index(result, name=self.name) @cache_readonly def dtype(self) -> DtypeObj: @@ -2493,8 +2484,7 @@ def _is_multi(self) -> bool: # Pickle Methods def __reduce__(self): - d = {"data": self._data} - d.update(self._get_attributes_dict()) + d = {"data": self._data, "name": self.name} return _new_Index, (type(self), d), None # -------------------------------------------------------------------- @@ -5820,29 +5810,29 @@ def map(self, mapper, na_action=None): new_values = self._map_values(mapper, na_action=na_action) - attributes = self._get_attributes_dict() - # we can return a MultiIndex if new_values.size and isinstance(new_values[0], tuple): if isinstance(self, MultiIndex): names = self.names - elif attributes.get("name"): - names = [attributes.get("name")] * len(new_values[0]) + elif self.name: + names = [self.name] * len(new_values[0]) else: names = None return MultiIndex.from_tuples(new_values, names=names) - attributes["copy"] = False + dtype = None if not new_values.size: # empty - attributes["dtype"] = self.dtype + dtype = self.dtype if self._is_backward_compat_public_numeric_index and is_numeric_dtype( new_values.dtype ): - return self._constructor(new_values, **attributes) + return self._constructor( + new_values, dtype=dtype, copy=False, name=self.name + ) - return Index._with_infer(new_values, **attributes) + return Index._with_infer(new_values, dtype=dtype, copy=False, name=self.name) # TODO: De-duplicate with map, xref GH#32349 @final diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index c45543a9187bd..02bbfe69be1b8 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -194,8 +194,6 @@ def _engine_type(self): np.int64: libindex.Int64Engine, }[self.codes.dtype.type] - _attributes = ["name"] - # -------------------------------------------------------------------- # Constructors diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index d556466554ea4..6078da3bedd8c 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -357,12 +357,7 @@ def _is_dates_only(self) -> bool: return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type] def __reduce__(self): - - # we use a special reduce here because we need - # to simply set the .tz (and not reinterpret it) - - d = {"data": self._data} - d.update(self._get_attributes_dict()) + d = {"data": self._data, "name": self.name} return _new_DatetimeIndex, (type(self), d), None def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index f494638ba1aa4..165048e2a591a 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -354,8 +354,12 @@ def _multiindex(self) -> MultiIndex: return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"]) def __reduce__(self): - d = {"left": self.left, "right": self.right, "closed": self.closed} - d.update(self._get_attributes_dict()) + d = { + "left": self.left, + "right": self.right, + "closed": self.closed, + "name": self.name, + } return _new_IntervalIndex, (type(self), d), None @property diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 4c4902d3ce89f..e422f2bc3ff9a 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -148,7 +148,6 @@ class PeriodIndex(DatetimeIndexOpsMixin): """ _typ = "periodindex" - _attributes = ["name"] _data: PeriodArray freq: BaseOffset diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 71bc4af78db6b..51d9f15390789 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -206,7 +206,7 @@ def _get_data_as_items(self): return [("start", rng.start), ("stop", rng.stop), ("step", rng.step)] def __reduce__(self): - d = self._get_attributes_dict() + d = {"name": self.name} d.update(dict(self._get_data_as_items())) return ibase._new_Index, (type(self), d), None @@ -913,7 +913,6 @@ def _arith_method(self, other, op): # TODO: if other is a RangeIndex we may have more efficient options other = extract_array(other, extract_numpy=True, extract_range=True) - attrs = self._get_attributes_dict() left, right = self, other @@ -935,7 +934,7 @@ def _arith_method(self, other, op): rstart = op(left.start, right) rstop = op(left.stop, right) - result = type(self)(rstart, rstop, rstep, **attrs) + result = type(self)(rstart, rstop, rstep, name=self.name) # for compat with numpy / Int64Index # even if we can represent as a RangeIndex, return