@@ -150,7 +150,7 @@ cpdef convert_to_timedelta64(object ts, object unit):
150
150
ts = np.timedelta64(delta_to_nanoseconds(ts), ' ns' )
151
151
elif not is_timedelta64_object(ts):
152
152
raise ValueError (" Invalid type for timedelta "
153
- " scalar: %s " % type (ts))
153
+ " scalar: {ts_type} " .format( ts_type = type (ts) ))
154
154
return ts.astype(' timedelta64[ns]' )
155
155
156
156
@@ -526,7 +526,7 @@ cdef class _Timedelta(timedelta):
526
526
int64_t value # nanoseconds
527
527
object freq # frequency reference
528
528
bint is_populated # are my components populated
529
- int64_t _sign, _d, _h, _m, _s, _ms, _us, _ns
529
+ int64_t _d, _h, _m, _s, _ms, _us, _ns
530
530
531
531
# higher than np.ndarray and np.matrix
532
532
__array_priority__ = 100
@@ -560,9 +560,9 @@ cdef class _Timedelta(timedelta):
560
560
return True
561
561
562
562
# only allow ==, != ops
563
- raise TypeError (' Cannot compare type %r with type %r ' %
564
- (type (self ).__name__,
565
- type (other).__name__))
563
+ raise TypeError (' Cannot compare type {!r} with type ' \
564
+ ' {!r} ' .format (type (self ).__name__,
565
+ type (other).__name__))
566
566
if util.is_array(other):
567
567
return PyObject_RichCompare(np.array([self ]), other, op)
568
568
return PyObject_RichCompare(other, self , reverse_ops[op])
@@ -571,8 +571,9 @@ cdef class _Timedelta(timedelta):
571
571
return False
572
572
elif op == Py_NE:
573
573
return True
574
- raise TypeError (' Cannot compare type %r with type %r ' %
575
- (type (self ).__name__, type (other).__name__))
574
+ raise TypeError (' Cannot compare type {!r} with type ' \
575
+ ' {!r}' .format(type (self ).__name__,
576
+ type (other).__name__))
576
577
577
578
return cmp_scalar(self .value, ots.value, op)
578
579
@@ -591,10 +592,6 @@ cdef class _Timedelta(timedelta):
591
592
592
593
td64_to_tdstruct(self .value, & tds)
593
594
self ._d = tds.days
594
- if self ._d < 0 :
595
- self ._sign = - 1
596
- else :
597
- self ._sign = 1
598
595
self ._h = tds.hrs
599
596
self ._m = tds.min
600
597
self ._s = tds.sec
@@ -680,60 +677,47 @@ cdef class _Timedelta(timedelta):
680
677
681
678
Parameters
682
679
----------
683
- format : None|all|even_day| sub_day|long
680
+ format : None|all|sub_day|long
684
681
685
682
Returns
686
683
-------
687
684
converted : string of a Timedelta
688
685
689
686
"""
690
- cdef object sign_pretty, sign2_pretty, seconds_pretty, subs
687
+ cdef object sign, seconds_pretty, subs, fmt, comp_dict
691
688
692
689
self ._ensure_components()
693
690
694
- if self ._sign < 0 :
695
- sign_pretty = " -"
696
- sign2_pretty = " +"
691
+ if self ._d < 0 :
692
+ sign = " +"
697
693
else :
698
- sign_pretty = " "
699
- sign2_pretty = " "
694
+ sign = " "
700
695
701
- # show everything
702
696
if format == ' all' :
703
- seconds_pretty = " %02d .%03d%03d%03d " % (
704
- self ._s, self ._ms, self ._us, self ._ns)
705
- return " %d days%s%02d :%02d :%s " % (self ._d,
706
- sign2_pretty, self ._h,
707
- self ._m, seconds_pretty)
708
-
709
- # by default not showing nano
710
- if self ._ms or self ._us or self ._ns:
711
- seconds_pretty = " %02d .%03d%03d " % (self ._s, self ._ms, self ._us)
697
+ fmt = " {days} days{sign}{hours:02}:{minutes:02}:{seconds:02}." \
698
+ " {milliseconds:03}{microseconds:03}{nanoseconds:03}"
712
699
else :
713
- seconds_pretty = " %02d " % self ._s
714
-
715
- # if we have a partial day
716
- subs = (self ._h or self ._m or self ._s or
717
- self ._ms or self ._us or self ._ns)
718
-
719
- if format == ' even_day' :
720
- if not subs:
721
- return " %d days" % (self ._d)
722
-
723
- elif format == ' sub_day' :
724
- if not self ._d:
725
-
726
- # degenerate, don't need the extra space
727
- if self ._sign > 0 :
728
- sign2_pretty = " "
729
- return " %s%s%02d :%02d :%s " % (sign_pretty, sign2_pretty,
730
- self ._h, self ._m, seconds_pretty)
731
-
732
- if subs or format== ' long' :
733
- return " %d days%s%02d :%02d :%s " % (self ._d,
734
- sign2_pretty, self ._h,
735
- self ._m, seconds_pretty)
736
- return " %d days" % (self ._d)
700
+ # if we have a partial day
701
+ subs = (self ._h or self ._m or self ._s or
702
+ self ._ms or self ._us or self ._ns)
703
+
704
+ # by default not showing nano
705
+ if self ._ms or self ._us or self ._ns:
706
+ seconds_fmt = " {seconds:02}.{milliseconds:03}{microseconds:03}"
707
+ else :
708
+ seconds_fmt = " {seconds:02}"
709
+
710
+ if format == ' sub_day' and not self ._d:
711
+ fmt = " {hours:02}:{minutes:02}:" + seconds_fmt
712
+ elif subs or format == ' long' :
713
+ fmt = " {days} days{sign}{hours:02}:{minutes:02}:" + seconds_fmt
714
+ else :
715
+ fmt = " {days} days"
716
+
717
+ comp_dict = self .components._asdict()
718
+ comp_dict[' sign' ] = sign
719
+
720
+ return fmt.format(** comp_dict)
737
721
738
722
def __repr__ (self ):
739
723
return " Timedelta('{0}')" .format(self ._repr_base(format = ' long' ))
0 commit comments