1
1
from datetime import date , datetime , time , timedelta , tzinfo
2
2
import operator
3
- from typing import Optional
3
+ from typing import TYPE_CHECKING , Optional
4
4
import warnings
5
5
6
6
import numpy as np
30
30
from pandas .core .indexes .extension import inherit_names
31
31
from pandas .core .tools .times import to_time
32
32
33
+ if TYPE_CHECKING :
34
+ from pandas import DataFrame , Float64Index , PeriodIndex , TimedeltaIndex
35
+
33
36
34
37
def _new_DatetimeIndex (cls , d ):
35
38
"""
@@ -64,8 +67,7 @@ def _new_DatetimeIndex(cls, d):
64
67
65
68
66
69
@inherit_names (
67
- ["to_perioddelta" , "to_julian_date" , "strftime" , "isocalendar" ]
68
- + DatetimeArray ._field_ops
70
+ DatetimeArray ._field_ops
69
71
+ [
70
72
method
71
73
for method in DatetimeArray ._datetimelike_methods
@@ -220,7 +222,12 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
220
222
tz : Optional [tzinfo ]
221
223
222
224
# --------------------------------------------------------------------
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 )
224
231
225
232
@doc (DatetimeArray .tz_convert )
226
233
def tz_convert (self , tz ) -> "DatetimeIndex" :
@@ -235,9 +242,30 @@ def tz_localize(
235
242
return type (self )._simple_new (arr , name = self .name )
236
243
237
244
@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
+
239
248
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 )
241
269
242
270
# --------------------------------------------------------------------
243
271
# Constructors
0 commit comments