Skip to content

Commit 9e1ee2d

Browse files
jbrockmendelgasparitiago
authored andcommitted
REF: remove _get_attributes_dict (pandas-dev#43895)
1 parent d1a4408 commit 9e1ee2d

File tree

6 files changed

+19
-34
lines changed

6 files changed

+19
-34
lines changed

pandas/core/indexes/base.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,6 @@ def _format_duplicate_message(self) -> DataFrame:
728728
# --------------------------------------------------------------------
729729
# Index Internals Methods
730730

731-
@final
732-
def _get_attributes_dict(self) -> dict[str_t, Any]:
733-
"""
734-
Return an attributes dict for my class.
735-
"""
736-
return {k: getattr(self, k, None) for k in self._attributes}
737-
738731
def _shallow_copy(self: _IndexT, values, name: Hashable = no_default) -> _IndexT:
739732
"""
740733
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):
859852
if is_bool_dtype(result) or lib.is_scalar(result) or np.ndim(result) > 1:
860853
return result
861854

862-
attrs = self._get_attributes_dict()
863-
attrs.pop("freq", None) # For DatetimeIndex/TimedeltaIndex
864-
return Index(result, **attrs)
855+
return Index(result, name=self.name)
865856

866857
@cache_readonly
867858
def dtype(self) -> DtypeObj:
@@ -2493,8 +2484,7 @@ def _is_multi(self) -> bool:
24932484
# Pickle Methods
24942485

24952486
def __reduce__(self):
2496-
d = {"data": self._data}
2497-
d.update(self._get_attributes_dict())
2487+
d = {"data": self._data, "name": self.name}
24982488
return _new_Index, (type(self), d), None
24992489

25002490
# --------------------------------------------------------------------
@@ -5820,29 +5810,29 @@ def map(self, mapper, na_action=None):
58205810

58215811
new_values = self._map_values(mapper, na_action=na_action)
58225812

5823-
attributes = self._get_attributes_dict()
5824-
58255813
# we can return a MultiIndex
58265814
if new_values.size and isinstance(new_values[0], tuple):
58275815
if isinstance(self, MultiIndex):
58285816
names = self.names
5829-
elif attributes.get("name"):
5830-
names = [attributes.get("name")] * len(new_values[0])
5817+
elif self.name:
5818+
names = [self.name] * len(new_values[0])
58315819
else:
58325820
names = None
58335821
return MultiIndex.from_tuples(new_values, names=names)
58345822

5835-
attributes["copy"] = False
5823+
dtype = None
58365824
if not new_values.size:
58375825
# empty
5838-
attributes["dtype"] = self.dtype
5826+
dtype = self.dtype
58395827

58405828
if self._is_backward_compat_public_numeric_index and is_numeric_dtype(
58415829
new_values.dtype
58425830
):
5843-
return self._constructor(new_values, **attributes)
5831+
return self._constructor(
5832+
new_values, dtype=dtype, copy=False, name=self.name
5833+
)
58445834

5845-
return Index._with_infer(new_values, **attributes)
5835+
return Index._with_infer(new_values, dtype=dtype, copy=False, name=self.name)
58465836

58475837
# TODO: De-duplicate with map, xref GH#32349
58485838
@final

pandas/core/indexes/category.py

-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ def _engine_type(self):
194194
np.int64: libindex.Int64Engine,
195195
}[self.codes.dtype.type]
196196

197-
_attributes = ["name"]
198-
199197
# --------------------------------------------------------------------
200198
# Constructors
201199

pandas/core/indexes/datetimes.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,7 @@ def _is_dates_only(self) -> bool:
357357
return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type]
358358

359359
def __reduce__(self):
360-
361-
# we use a special reduce here because we need
362-
# to simply set the .tz (and not reinterpret it)
363-
364-
d = {"data": self._data}
365-
d.update(self._get_attributes_dict())
360+
d = {"data": self._data, "name": self.name}
366361
return _new_DatetimeIndex, (type(self), d), None
367362

368363
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:

pandas/core/indexes/interval.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,12 @@ def _multiindex(self) -> MultiIndex:
354354
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])
355355

356356
def __reduce__(self):
357-
d = {"left": self.left, "right": self.right, "closed": self.closed}
358-
d.update(self._get_attributes_dict())
357+
d = {
358+
"left": self.left,
359+
"right": self.right,
360+
"closed": self.closed,
361+
"name": self.name,
362+
}
359363
return _new_IntervalIndex, (type(self), d), None
360364

361365
@property

pandas/core/indexes/period.py

-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ class PeriodIndex(DatetimeIndexOpsMixin):
148148
"""
149149

150150
_typ = "periodindex"
151-
_attributes = ["name"]
152151

153152
_data: PeriodArray
154153
freq: BaseOffset

pandas/core/indexes/range.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def _get_data_as_items(self):
206206
return [("start", rng.start), ("stop", rng.stop), ("step", rng.step)]
207207

208208
def __reduce__(self):
209-
d = self._get_attributes_dict()
209+
d = {"name": self.name}
210210
d.update(dict(self._get_data_as_items()))
211211
return ibase._new_Index, (type(self), d), None
212212

@@ -913,7 +913,6 @@ def _arith_method(self, other, op):
913913

914914
# TODO: if other is a RangeIndex we may have more efficient options
915915
other = extract_array(other, extract_numpy=True, extract_range=True)
916-
attrs = self._get_attributes_dict()
917916

918917
left, right = self, other
919918

@@ -935,7 +934,7 @@ def _arith_method(self, other, op):
935934
rstart = op(left.start, right)
936935
rstop = op(left.stop, right)
937936

938-
result = type(self)(rstart, rstop, rstep, **attrs)
937+
result = type(self)(rstart, rstop, rstep, name=self.name)
939938

940939
# for compat with numpy / Int64Index
941940
# even if we can represent as a RangeIndex, return

0 commit comments

Comments
 (0)