Skip to content

TYP: some more static definitions of methods for DatetimeIndex #36742

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
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
40 changes: 34 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import date, datetime, time, timedelta, tzinfo
import operator
from typing import Optional
from typing import TYPE_CHECKING, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -30,6 +30,9 @@
from pandas.core.indexes.extension import inherit_names
from pandas.core.tools.times import to_time

if TYPE_CHECKING:
from pandas import DataFrame, Float64Index, PeriodIndex, TimedeltaIndex


def _new_DatetimeIndex(cls, d):
"""
Expand Down Expand Up @@ -64,8 +67,7 @@ def _new_DatetimeIndex(cls, d):


@inherit_names(
["to_perioddelta", "to_julian_date", "strftime", "isocalendar"]
+ DatetimeArray._field_ops
DatetimeArray._field_ops
+ [
method
for method in DatetimeArray._datetimelike_methods
Expand Down Expand Up @@ -220,7 +222,12 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
tz: Optional[tzinfo]

# --------------------------------------------------------------------
# methods that dispatch to array and wrap result in DatetimeIndex
# methods that dispatch to DatetimeArray and wrap result

@doc(DatetimeArray.strftime)
def strftime(self, date_format) -> Index:
arr = self._data.strftime(date_format)
return Index(arr, name=self.name)

@doc(DatetimeArray.tz_convert)
def tz_convert(self, tz) -> "DatetimeIndex":
Expand All @@ -235,9 +242,30 @@ def tz_localize(
return type(self)._simple_new(arr, name=self.name)

@doc(DatetimeArray.to_period)
def to_period(self, freq=None) -> "DatetimeIndex":
def to_period(self, freq=None) -> "PeriodIndex":
from pandas.core.indexes.api import PeriodIndex

arr = self._data.to_period(freq)
return type(self)._simple_new(arr, name=self.name)
return PeriodIndex._simple_new(arr, name=self.name)

@doc(DatetimeArray.to_perioddelta)
def to_perioddelta(self, freq) -> "TimedeltaIndex":
from pandas.core.indexes.api import TimedeltaIndex

arr = self._data.to_perioddelta(freq)
return TimedeltaIndex._simple_new(arr, name=self.name)

@doc(DatetimeArray.to_julian_date)
def to_julian_date(self) -> "Float64Index":
from pandas.core.indexes.api import Float64Index

arr = self._data.to_julian_date()
return Float64Index._simple_new(arr, name=self.name)

@doc(DatetimeArray.isocalendar)
def isocalendar(self) -> "DataFrame":
df = self._data.isocalendar()
return df.set_index(self)

# --------------------------------------------------------------------
# Constructors
Expand Down