Skip to content

Commit 0cedcbf

Browse files
authored
TYP: avoid inherit_names for DatetimeIndexOpsMixin (#49804)
1 parent c75f5af commit 0cedcbf

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

pandas/core/indexes/datetimelike.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"""
44
from __future__ import annotations
55

6+
from abc import (
7+
ABC,
8+
abstractmethod,
9+
)
610
from datetime import datetime
711
from typing import (
812
TYPE_CHECKING,
@@ -61,10 +65,7 @@
6165
Index,
6266
_index_shared_docs,
6367
)
64-
from pandas.core.indexes.extension import (
65-
NDArrayBackedExtensionIndex,
66-
inherit_names,
67-
)
68+
from pandas.core.indexes.extension import NDArrayBackedExtensionIndex
6869
from pandas.core.indexes.range import RangeIndex
6970
from pandas.core.tools.timedeltas import to_timedelta
7071

@@ -77,21 +78,17 @@
7778
_TDT = TypeVar("_TDT", bound="DatetimeTimedeltaMixin")
7879

7980

80-
@inherit_names(
81-
["inferred_freq", "_resolution_obj", "resolution"],
82-
DatetimeLikeArrayMixin,
83-
cache=True,
84-
)
85-
@inherit_names(["mean", "freqstr"], DatetimeLikeArrayMixin)
86-
class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex):
81+
class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC):
8782
"""
8883
Common ops mixin to support a unified interface datetimelike Index.
8984
"""
9085

9186
_can_hold_strings = False
9287
_data: DatetimeArray | TimedeltaArray | PeriodArray
93-
freqstr: str | None
94-
_resolution_obj: Resolution
88+
89+
@doc(DatetimeLikeArrayMixin.mean)
90+
def mean(self, *, skipna: bool = True, axis: int | None = 0):
91+
return self._data.mean(skipna=skipna, axis=axis)
9592

9693
@property
9794
def freq(self) -> BaseOffset | None:
@@ -106,6 +103,21 @@ def freq(self, value) -> None:
106103
def asi8(self) -> npt.NDArray[np.int64]:
107104
return self._data.asi8
108105

106+
@property
107+
@doc(DatetimeLikeArrayMixin.freqstr)
108+
def freqstr(self) -> str | None:
109+
return self._data.freqstr
110+
111+
@cache_readonly
112+
@abstractmethod
113+
def _resolution_obj(self) -> Resolution:
114+
...
115+
116+
@cache_readonly
117+
@doc(DatetimeLikeArrayMixin.resolution)
118+
def resolution(self) -> str:
119+
return self._data.resolution
120+
109121
# ------------------------------------------------------------------------
110122

111123
@cache_readonly
@@ -390,7 +402,7 @@ def _maybe_cast_listlike_indexer(self, keyarr):
390402
return Index(res, dtype=res.dtype)
391403

392404

393-
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
405+
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, ABC):
394406
"""
395407
Mixin class for methods shared by DatetimeIndex and TimedeltaIndex,
396408
but not PeriodIndex
@@ -461,6 +473,11 @@ def shift(self: _TDT, periods: int = 1, freq=None) -> _TDT:
461473
)
462474
return type(self)._simple_new(result, name=self.name)
463475

476+
@cache_readonly
477+
@doc(DatetimeLikeArrayMixin.inferred_freq)
478+
def inferred_freq(self) -> str | None:
479+
return self._data.inferred_freq
480+
464481
# --------------------------------------------------------------------
465482
# Set Operation Methods
466483

pandas/core/indexes/datetimes.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _new_DatetimeIndex(cls, d):
109109
DatetimeArray,
110110
wrap=True,
111111
)
112-
@inherit_names(["is_normalized", "_resolution_obj"], DatetimeArray, cache=True)
112+
@inherit_names(["is_normalized"], DatetimeArray, cache=True)
113113
@inherit_names(
114114
[
115115
"tz",
@@ -248,7 +248,6 @@ def _engine_type(self) -> type[libindex.DatetimeEngine]:
248248
return libindex.DatetimeEngine
249249

250250
_data: DatetimeArray
251-
inferred_freq: str | None
252251
tz: dt.tzinfo | None
253252

254253
# --------------------------------------------------------------------
@@ -291,6 +290,10 @@ def isocalendar(self) -> DataFrame:
291290
df = self._data.isocalendar()
292291
return df.set_index(self)
293292

293+
@cache_readonly
294+
def _resolution_obj(self) -> Resolution:
295+
return self._data._resolution_obj
296+
294297
# --------------------------------------------------------------------
295298
# Constructors
296299

pandas/core/indexes/period.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ def _engine_type(self) -> type[libindex.PeriodEngine]:
156156
return libindex.PeriodEngine
157157

158158
@cache_readonly
159-
# Signature of "_resolution_obj" incompatible with supertype "DatetimeIndexOpsMixin"
160-
def _resolution_obj(self) -> Resolution: # type: ignore[override]
159+
def _resolution_obj(self) -> Resolution:
161160
# for compat with DatetimeIndex
162161
return self.dtype._resolution_obj
163162

pandas/core/indexes/timedeltas.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
lib,
77
)
88
from pandas._libs.tslibs import (
9+
Resolution,
910
Timedelta,
1011
to_offset,
1112
)
@@ -112,6 +113,12 @@ def _engine_type(self) -> type[libindex.TimedeltaEngine]:
112113
# Use base class method instead of DatetimeTimedeltaMixin._get_string_slice
113114
_get_string_slice = Index._get_string_slice
114115

116+
# error: Signature of "_resolution_obj" incompatible with supertype
117+
# "DatetimeIndexOpsMixin"
118+
@property
119+
def _resolution_obj(self) -> Resolution | None: # type: ignore[override]
120+
return self._data._resolution_obj
121+
115122
# -------------------------------------------------------------------
116123
# Constructors
117124

0 commit comments

Comments
 (0)