Skip to content

TYP: avoid inherit_names for DatetimeIndexOpsMixin #49804

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
Feb 4, 2023
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
45 changes: 31 additions & 14 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"""
from __future__ import annotations

from abc import (
ABC,
abstractmethod,
)
from datetime import datetime
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -61,10 +65,7 @@
Index,
_index_shared_docs,
)
from pandas.core.indexes.extension import (
NDArrayBackedExtensionIndex,
inherit_names,
)
from pandas.core.indexes.extension import NDArrayBackedExtensionIndex
from pandas.core.indexes.range import RangeIndex
from pandas.core.tools.timedeltas import to_timedelta

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


@inherit_names(
["inferred_freq", "_resolution_obj", "resolution"],
DatetimeLikeArrayMixin,
cache=True,
)
@inherit_names(["mean", "freqstr"], DatetimeLikeArrayMixin)
class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex):
class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC):
"""
Common ops mixin to support a unified interface datetimelike Index.
"""

_can_hold_strings = False
_data: DatetimeArray | TimedeltaArray | PeriodArray
freqstr: str | None
_resolution_obj: Resolution

@doc(DatetimeLikeArrayMixin.mean)
def mean(self, *, skipna: bool = True, axis: int | None = 0):
return self._data.mean(skipna=skipna, axis=axis)

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

@property
@doc(DatetimeLikeArrayMixin.freqstr)
def freqstr(self) -> str | None:
return self._data.freqstr

@cache_readonly
@abstractmethod
def _resolution_obj(self) -> Resolution:
...

@cache_readonly
@doc(DatetimeLikeArrayMixin.resolution)
def resolution(self) -> str:
return self._data.resolution

# ------------------------------------------------------------------------

@cache_readonly
Expand Down Expand Up @@ -390,7 +402,7 @@ def _maybe_cast_listlike_indexer(self, keyarr):
return Index(res, dtype=res.dtype)


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

@cache_readonly
@doc(DatetimeLikeArrayMixin.inferred_freq)
def inferred_freq(self) -> str | None:
return self._data.inferred_freq

# --------------------------------------------------------------------
# Set Operation Methods

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _new_DatetimeIndex(cls, d):
DatetimeArray,
wrap=True,
)
@inherit_names(["is_normalized", "_resolution_obj"], DatetimeArray, cache=True)
@inherit_names(["is_normalized"], DatetimeArray, cache=True)
@inherit_names(
[
"tz",
Expand Down Expand Up @@ -249,7 +249,6 @@ def _engine_type(self) -> type[libindex.DatetimeEngine]:
return libindex.DatetimeEngine

_data: DatetimeArray
inferred_freq: str | None
tz: dt.tzinfo | None

# --------------------------------------------------------------------
Expand Down Expand Up @@ -294,6 +293,10 @@ def isocalendar(self) -> DataFrame:
df = self._data.isocalendar()
return df.set_index(self)

@cache_readonly
def _resolution_obj(self) -> Resolution:
return self._data._resolution_obj

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

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def _engine_type(self) -> type[libindex.PeriodEngine]:
return libindex.PeriodEngine

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

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
lib,
)
from pandas._libs.tslibs import (
Resolution,
Timedelta,
to_offset,
)
Expand Down Expand Up @@ -112,6 +113,12 @@ def _engine_type(self) -> type[libindex.TimedeltaEngine]:
# Use base class method instead of DatetimeTimedeltaMixin._get_string_slice
_get_string_slice = Index._get_string_slice

# error: Signature of "_resolution_obj" incompatible with supertype
# "DatetimeIndexOpsMixin"
@property
def _resolution_obj(self) -> Resolution | None: # type: ignore[override]
return self._data._resolution_obj

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

Expand Down