@@ -162,7 +162,7 @@ cpdef convert_to_timedelta64(object ts, object unit):
162
162
if ts.astype(' int64' ) == NPY_NAT:
163
163
return np.timedelta64(NPY_NAT)
164
164
elif is_timedelta64_object(ts):
165
- ts = ts.astype(" m8[{0 }]" .format(unit.lower()))
165
+ ts = ts.astype(" m8[{unit }]" .format(unit = unit.lower()))
166
166
elif is_integer_object(ts):
167
167
if ts == NPY_NAT:
168
168
return np.timedelta64(NPY_NAT)
@@ -265,19 +265,19 @@ cpdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
265
265
m = 1L
266
266
p = 0
267
267
else :
268
- raise ValueError (" cannot cast unit {0 }" .format(unit))
268
+ raise ValueError (" cannot cast unit {unit }" .format(unit = unit))
269
269
270
270
# just give me the unit back
271
271
if ts is None :
272
272
return m
273
273
274
274
# cast the unit, multiply base/frace separately
275
275
# to avoid precision issues from float -> int
276
- base = < int64_t> ts
276
+ base = < int64_t> ts
277
277
frac = ts - base
278
278
if p:
279
279
frac = round (frac, p)
280
- return < int64_t> (base * m) + < int64_t> (frac * m)
280
+ return < int64_t> (base * m) + < int64_t> (frac * m)
281
281
282
282
283
283
cdef inline _decode_if_necessary(object ts):
@@ -288,19 +288,18 @@ cdef inline _decode_if_necessary(object ts):
288
288
return ts
289
289
290
290
291
- cdef inline parse_timedelta_string(object ts, specified_unit = None ):
291
+ cdef inline parse_timedelta_string(object ts):
292
292
"""
293
293
Parse a regular format timedelta string. Return an int64_t (in ns)
294
294
or raise a ValueError on an invalid parse.
295
295
"""
296
296
297
297
cdef:
298
298
unicode c
299
- bint neg= 0 , have_dot= 0 , have_value= 0 , have_hhmmss= 0
300
- object current_unit= None
301
- object fallback_unit= None
302
- int64_t result= 0 , m= 0 , r
303
- list number= [], frac= [], unit= []
299
+ bint neg = 0 , have_dot = 0 , have_value = 0 , have_hhmmss = 0
300
+ object current_unit = None
301
+ int64_t result = 0 , m = 0 , r
302
+ list number = [], frac = [], unit = []
304
303
305
304
# neg : tracks if we have a leading negative for the value
306
305
# have_dot : tracks if we are processing a dot (either post hhmmss or
@@ -375,7 +374,7 @@ cdef inline parse_timedelta_string(object ts, specified_unit=None):
375
374
have_hhmmss = 1
376
375
else :
377
376
raise ValueError (" expecting hh:mm:ss format, "
378
- " received: {0 }" .format(ts))
377
+ " received: {ts }" .format(ts = ts))
379
378
380
379
unit, number = [], []
381
380
@@ -450,13 +449,9 @@ cdef inline parse_timedelta_string(object ts, specified_unit=None):
450
449
if have_value:
451
450
raise ValueError (" have leftover units" )
452
451
if len (number):
453
- fallback_unit = ' ns' if specified_unit is None else specified_unit
454
- r = timedelta_from_spec(number, frac, fallback_unit)
452
+ r = timedelta_from_spec(number, frac, ' ns' )
455
453
result += timedelta_as_neg(r, neg)
456
454
457
- if (specified_unit is not None ) and (fallback_unit is None ):
458
- raise ValueError (" unit was specified but is redundant/ambiguous" )
459
-
460
455
return result
461
456
462
457
@@ -488,7 +483,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
488
483
unit = ' ' .join(unit)
489
484
unit = timedelta_abbrevs[unit.lower()]
490
485
except KeyError :
491
- raise ValueError (" invalid abbreviation: {0 }" .format(unit))
486
+ raise ValueError (" invalid abbreviation: {unit }" .format(unit = unit))
492
487
493
488
n = ' ' .join(number) + ' .' + ' ' .join(frac)
494
489
return cast_from_unit(float (n), unit)
@@ -597,10 +592,10 @@ cdef inline int64_t parse_iso_format_string(object ts) except? -1:
597
592
cdef:
598
593
unicode c
599
594
int64_t result = 0 , r
600
- int p= 0
595
+ int p = 0
601
596
object dec_unit = ' ms' , err_msg
602
- bint have_dot= 0 , have_value= 0 , neg= 0
603
- list number= [], unit= []
597
+ bint have_dot = 0 , have_value = 0 , neg = 0
598
+ list number = [], unit = []
604
599
605
600
ts = _decode_if_necessary(ts)
606
601
@@ -687,8 +682,8 @@ cdef _to_py_int_float(v):
687
682
return int (v)
688
683
elif is_float_object(v):
689
684
return float (v)
690
- raise TypeError (" Invalid type {0 }. Must be int or "
691
- " float." .format(type (v)))
685
+ raise TypeError (" Invalid type {typ }. Must be int or "
686
+ " float." .format(typ = type (v)))
692
687
693
688
694
689
# Similar to Timestamp/datetime, this is a construction requirement for
@@ -734,9 +729,10 @@ cdef class _Timedelta(timedelta):
734
729
return True
735
730
736
731
# only allow ==, != ops
737
- raise TypeError (' Cannot compare type {!r} with type ' \
738
- ' {!r}' .format(type (self ).__name__,
739
- type (other).__name__))
732
+ raise TypeError (' Cannot compare type {cls} with '
733
+ ' type {other}'
734
+ .format(cls = type (self ).__name__,
735
+ other = type (other).__name__))
740
736
if util.is_array(other):
741
737
return PyObject_RichCompare(np.array([self ]), other, op)
742
738
return PyObject_RichCompare(other, self , reverse_ops[op])
@@ -745,9 +741,9 @@ cdef class _Timedelta(timedelta):
745
741
return False
746
742
elif op == Py_NE:
747
743
return True
748
- raise TypeError (' Cannot compare type {!r } with type ' \
749
- ' {!r} ' .format(type (self ).__name__,
750
- type (other).__name__))
744
+ raise TypeError (' Cannot compare type {cls } with type {other} '
745
+ .format(cls = type (self ).__name__,
746
+ other = type (other).__name__))
751
747
752
748
return cmp_scalar(self .value, ots.value, op)
753
749
@@ -985,8 +981,8 @@ cdef class _Timedelta(timedelta):
985
981
sign = " "
986
982
987
983
if format == ' all' :
988
- fmt = " {days} days{sign}{hours:02}:{minutes:02}:{seconds:02}." \
989
- " {milliseconds:03}{microseconds:03}{nanoseconds:03}"
984
+ fmt = ( " {days} days{sign}{hours:02}:{minutes:02}:{seconds:02}."
985
+ " {milliseconds:03}{microseconds:03}{nanoseconds:03}" )
990
986
else :
991
987
# if we have a partial day
992
988
subs = (self ._h or self ._m or self ._s or
@@ -1011,7 +1007,7 @@ cdef class _Timedelta(timedelta):
1011
1007
return fmt.format(** comp_dict)
1012
1008
1013
1009
def __repr__ (self ):
1014
- return " Timedelta('{0 }')" .format(self ._repr_base(format = ' long' ))
1010
+ return " Timedelta('{val }')" .format(val = self ._repr_base(format = ' long' ))
1015
1011
1016
1012
def __str__ (self ):
1017
1013
return self ._repr_base(format = ' long' )
@@ -1065,8 +1061,8 @@ cdef class _Timedelta(timedelta):
1065
1061
components.nanoseconds)
1066
1062
# Trim unnecessary 0s, 1.000000000 -> 1
1067
1063
seconds = seconds.rstrip(' 0' ).rstrip(' .' )
1068
- tpl = ' P{td.days}DT{td.hours}H{td.minutes}M{seconds}S' .format(
1069
- td = components, seconds = seconds)
1064
+ tpl = ( ' P{td.days}DT{td.hours}H{td.minutes}M{seconds}S'
1065
+ .format( td = components, seconds = seconds) )
1070
1066
return tpl
1071
1067
1072
1068
@@ -1119,21 +1115,10 @@ class Timedelta(_Timedelta):
1119
1115
if isinstance (value, Timedelta):
1120
1116
value = value.value
1121
1117
elif is_string_object(value):
1122
- # Check if it is just a number in a string
1123
- try :
1124
- value = int (value)
1125
- except (ValueError , TypeError ):
1126
- try :
1127
- value = float (value)
1128
- except (ValueError , TypeError ):
1129
- pass
1130
-
1131
- if is_integer_object(value) or is_float_object(value):
1132
- value = convert_to_timedelta64(value, unit)
1133
- elif len (value) > 0 and value[0 ] == ' P' :
1118
+ if len (value) > 0 and value[0 ] == ' P' :
1134
1119
value = parse_iso_format_string(value)
1135
1120
else :
1136
- value = parse_timedelta_string(value, unit )
1121
+ value = parse_timedelta_string(value)
1137
1122
value = np.timedelta64(value)
1138
1123
elif PyDelta_Check(value):
1139
1124
value = convert_to_timedelta64(value, ' ns' )
0 commit comments