Skip to content

REF: remove _get_attributes_dict #43895

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

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

# --------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ def _engine_type(self):
np.int64: libindex.Int64Engine,
}[self.codes.dtype.type]

_attributes = ["name"]

# --------------------------------------------------------------------
# Constructors

Expand Down
7 changes: 1 addition & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class PeriodIndex(DatetimeIndexOpsMixin):
"""

_typ = "periodindex"
_attributes = ["name"]

_data: PeriodArray
freq: BaseOffset
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down