@@ -2061,7 +2061,10 @@ cdef class _Timedelta(timedelta):
20612061 int64_t _sign, _d, _h, _m, _s, _ms, _us, _ns
20622062
20632063 def __hash__ (_Timedelta self ):
2064- return hash (self .value)
2064+ if self ._has_ns():
2065+ return hash (self .value)
2066+ else :
2067+ return timedelta.__hash__ (self )
20652068
20662069 def __richcmp__ (_Timedelta self , object other , int op ):
20672070 cdef:
@@ -2110,63 +2113,63 @@ cdef class _Timedelta(timedelta):
21102113 cdef float64_t frac
21112114
21122115 if self .is_populated:
2113- return
2116+ return
21142117
21152118 # put frac in seconds
2116- frac = float (ivalue)/ 1e9
2119+ frac = float (ivalue)/ 1e9
21172120 if frac < 0 :
2118- self ._sign = - 1
2119-
2120- # even fraction
2121- if int (- frac/ 86400 ) != - frac/ 86400.0 :
2122- self ._d = int (- frac/ 86400.0 + 1 )
2123- frac += 86400 * self ._d
2124- else :
2125- frac = - frac
2121+ self ._sign = - 1
2122+
2123+ # even fraction
2124+ if int (- frac/ 86400 ) != - frac/ 86400.0 :
2125+ self ._d = int (- frac/ 86400.0 + 1 )
2126+ frac += 86400 * self ._d
2127+ else :
2128+ frac = - frac
21262129 else :
2127- self ._sign = 1
2128- self ._d = 0
2130+ self ._sign = 1
2131+ self ._d = 0
21292132
21302133 if frac >= 86400 :
2131- self ._d += int (frac / 86400 )
2132- frac -= self ._d * 86400
2134+ self ._d += int (frac / 86400 )
2135+ frac -= self ._d * 86400
21332136
21342137 if frac >= 3600 :
2135- self ._h = int (frac / 3600 )
2136- frac -= self ._h * 3600
2138+ self ._h = int (frac / 3600 )
2139+ frac -= self ._h * 3600
21372140 else :
2138- self ._h = 0
2141+ self ._h = 0
21392142
21402143 if frac >= 60 :
2141- self ._m = int (frac / 60 )
2142- frac -= self ._m * 60
2144+ self ._m = int (frac / 60 )
2145+ frac -= self ._m * 60
21432146 else :
2144- self ._m = 0
2147+ self ._m = 0
21452148
21462149 if frac >= 0 :
2147- self ._s = int (frac)
2148- frac -= self ._s
2150+ self ._s = int (frac)
2151+ frac -= self ._s
21492152 else :
2150- self ._s = 0
2153+ self ._s = 0
21512154
21522155 if frac != 0 :
21532156
2154- # reset so we don't lose precision
2155- sfrac = int ((self ._h* 3600 + self ._m* 60 + self ._s)* 1e9 )
2156- if self ._sign < 0 :
2157- ifrac = ivalue + self ._d* DAY_NS - sfrac
2158- else :
2159- ifrac = ivalue - (self ._d* DAY_NS + sfrac)
2160-
2161- self ._ms = int (ifrac/ 1e6 )
2162- ifrac -= self ._ms* 1000 * 1000
2163- self ._us = int (ifrac/ 1e3 )
2164- ifrac -= self ._us* 1000
2165- self ._ns = ifrac
2157+ # reset so we don't lose precision
2158+ sfrac = int ((self ._h* 3600 + self ._m* 60 + self ._s)* 1e9 )
2159+ if self ._sign < 0 :
2160+ ifrac = ivalue + self ._d* DAY_NS - sfrac
2161+ else :
2162+ ifrac = ivalue - (self ._d* DAY_NS + sfrac)
2163+
2164+ self ._ms = int (ifrac/ 1e6 )
2165+ ifrac -= self ._ms* 1000 * 1000
2166+ self ._us = int (ifrac/ 1e3 )
2167+ ifrac -= self ._us* 1000
2168+ self ._ns = ifrac
21662169 else :
2167- self ._ms = 0
2168- self ._us = 0
2169- self ._ns = 0
2170+ self ._ms = 0
2171+ self ._us = 0
2172+ self ._ns = 0
21702173
21712174 self .is_populated = 1
21722175
@@ -2177,6 +2180,9 @@ cdef class _Timedelta(timedelta):
21772180 """
21782181 return timedelta(microseconds = int (self .value)/ 1000 )
21792182
2183+ cpdef bint _has_ns(self ):
2184+ return self .value % 1000 != 0
2185+
21802186# components named tuple
21812187Components = collections.namedtuple(' Components' ,[' days' ,' hours' ,' minutes' ,' seconds' ,' milliseconds' ,' microseconds' ,' nanoseconds' ])
21822188
@@ -2433,7 +2439,7 @@ class Timedelta(_Timedelta):
24332439 """
24342440 self ._ensure_components()
24352441 return self ._ns
2436-
2442+
24372443 def total_seconds (self ):
24382444 """
24392445 Total duration of timedelta in seconds (to ns precision)
0 commit comments