Skip to content

Commit a6d0968

Browse files
simonjayhawkinsKevin D Smith
authored and
Kevin D Smith
committed
TYP: some more static definitions of methods for DatetimeIndex (pandas-dev#36742)
1 parent e724c51 commit a6d0968

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

pandas/core/indexes/datetimes.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import date, datetime, time, timedelta, tzinfo
22
import operator
3-
from typing import Optional
3+
from typing import TYPE_CHECKING, Optional
44
import warnings
55

66
import numpy as np
@@ -30,6 +30,9 @@
3030
from pandas.core.indexes.extension import inherit_names
3131
from pandas.core.tools.times import to_time
3232

33+
if TYPE_CHECKING:
34+
from pandas import DataFrame, Float64Index, PeriodIndex, TimedeltaIndex
35+
3336

3437
def _new_DatetimeIndex(cls, d):
3538
"""
@@ -64,8 +67,7 @@ def _new_DatetimeIndex(cls, d):
6467

6568

6669
@inherit_names(
67-
["to_perioddelta", "to_julian_date", "strftime", "isocalendar"]
68-
+ DatetimeArray._field_ops
70+
DatetimeArray._field_ops
6971
+ [
7072
method
7173
for method in DatetimeArray._datetimelike_methods
@@ -220,7 +222,12 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
220222
tz: Optional[tzinfo]
221223

222224
# --------------------------------------------------------------------
223-
# methods that dispatch to array and wrap result in DatetimeIndex
225+
# methods that dispatch to DatetimeArray and wrap result
226+
227+
@doc(DatetimeArray.strftime)
228+
def strftime(self, date_format) -> Index:
229+
arr = self._data.strftime(date_format)
230+
return Index(arr, name=self.name)
224231

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

237244
@doc(DatetimeArray.to_period)
238-
def to_period(self, freq=None) -> "DatetimeIndex":
245+
def to_period(self, freq=None) -> "PeriodIndex":
246+
from pandas.core.indexes.api import PeriodIndex
247+
239248
arr = self._data.to_period(freq)
240-
return type(self)._simple_new(arr, name=self.name)
249+
return PeriodIndex._simple_new(arr, name=self.name)
250+
251+
@doc(DatetimeArray.to_perioddelta)
252+
def to_perioddelta(self, freq) -> "TimedeltaIndex":
253+
from pandas.core.indexes.api import TimedeltaIndex
254+
255+
arr = self._data.to_perioddelta(freq)
256+
return TimedeltaIndex._simple_new(arr, name=self.name)
257+
258+
@doc(DatetimeArray.to_julian_date)
259+
def to_julian_date(self) -> "Float64Index":
260+
from pandas.core.indexes.api import Float64Index
261+
262+
arr = self._data.to_julian_date()
263+
return Float64Index._simple_new(arr, name=self.name)
264+
265+
@doc(DatetimeArray.isocalendar)
266+
def isocalendar(self) -> "DataFrame":
267+
df = self._data.isocalendar()
268+
return df.set_index(self)
241269

242270
# --------------------------------------------------------------------
243271
# Constructors

0 commit comments

Comments
 (0)