11from datetime import timedelta
2- from typing import List , Union
2+ from typing import List , Optional , Union
33
44import numpy as np
55
66from pandas ._libs import lib , tslibs
77from pandas ._libs .tslibs import (
8+ BaseOffset ,
89 NaT ,
910 NaTType ,
1011 Period ,
4546from pandas .core .ops .common import unpack_zerodim_and_defer
4647
4748
48- def _field_accessor (name , alias , docstring = None ):
49- def f (self ):
49+ def _field_accessor (name : str , alias : str , docstring : str ):
50+ def f (self ) -> np . ndarray :
5051 values = self .asi8
5152 result = get_timedelta_field (values , alias )
5253 if self ._hasnans :
@@ -121,7 +122,7 @@ def _box_func(self, x) -> Union[Timedelta, NaTType]:
121122 return Timedelta (x , unit = "ns" )
122123
123124 @property
124- def dtype (self ):
125+ def dtype (self ) -> np . dtype :
125126 """
126127 The dtype for the TimedeltaArray.
127128
@@ -196,7 +197,9 @@ def __init__(self, values, dtype=TD64NS_DTYPE, freq=lib.no_default, copy=False):
196197 type (self )._validate_frequency (self , freq )
197198
198199 @classmethod
199- def _simple_new (cls , values , freq = None , dtype = TD64NS_DTYPE ):
200+ def _simple_new (
201+ cls , values , freq : Optional [BaseOffset ] = None , dtype = TD64NS_DTYPE
202+ ) -> "TimedeltaArray" :
200203 assert dtype == TD64NS_DTYPE , dtype
201204 assert isinstance (values , np .ndarray ), type (values )
202205 if values .dtype != TD64NS_DTYPE :
@@ -211,8 +214,13 @@ def _simple_new(cls, values, freq=None, dtype=TD64NS_DTYPE):
211214
212215 @classmethod
213216 def _from_sequence (
214- cls , data , dtype = TD64NS_DTYPE , copy = False , freq = lib .no_default , unit = None
215- ):
217+ cls ,
218+ data ,
219+ dtype = TD64NS_DTYPE ,
220+ copy : bool = False ,
221+ freq = lib .no_default ,
222+ unit = None ,
223+ ) -> "TimedeltaArray" :
216224 if dtype :
217225 _validate_td64_dtype (dtype )
218226
@@ -240,7 +248,9 @@ def _from_sequence(
240248 return result
241249
242250 @classmethod
243- def _generate_range (cls , start , end , periods , freq , closed = None ):
251+ def _generate_range (
252+ cls , start , end , periods , freq , closed = None
253+ ) -> "TimedeltaArray" :
244254
245255 periods = dtl .validate_periods (periods )
246256 if freq is None and any (x is None for x in [periods , start , end ]):
@@ -298,7 +308,7 @@ def _maybe_clear_freq(self):
298308 # ----------------------------------------------------------------
299309 # Array-Like / EA-Interface Methods
300310
301- def astype (self , dtype , copy = True ):
311+ def astype (self , dtype , copy : bool = True ):
302312 # We handle
303313 # --> timedelta64[ns]
304314 # --> timedelta64
@@ -461,7 +471,7 @@ def _addsub_object_array(self, other, op):
461471 ) from err
462472
463473 @unpack_zerodim_and_defer ("__mul__" )
464- def __mul__ (self , other ):
474+ def __mul__ (self , other ) -> "TimedeltaArray" :
465475 if is_scalar (other ):
466476 # numpy will accept float and int, raise TypeError for others
467477 result = self ._data * other
@@ -737,22 +747,22 @@ def __rdivmod__(self, other):
737747 res2 = other - res1 * self
738748 return res1 , res2
739749
740- def __neg__ (self ):
750+ def __neg__ (self ) -> "TimedeltaArray" :
741751 if self .freq is not None :
742752 return type (self )(- self ._data , freq = - self .freq )
743753 return type (self )(- self ._data )
744754
745- def __pos__ (self ):
755+ def __pos__ (self ) -> "TimedeltaArray" :
746756 return type (self )(self ._data , freq = self .freq )
747757
748- def __abs__ (self ):
758+ def __abs__ (self ) -> "TimedeltaArray" :
749759 # Note: freq is not preserved
750760 return type (self )(np .abs (self ._data ))
751761
752762 # ----------------------------------------------------------------
753763 # Conversion Methods - Vectorized analogues of Timedelta methods
754764
755- def total_seconds (self ):
765+ def total_seconds (self ) -> np . ndarray :
756766 """
757767 Return total duration of each element expressed in seconds.
758768
0 commit comments