@@ -150,7 +150,7 @@ cpdef convert_to_timedelta64(object ts, object unit):
150150 ts = np.timedelta64(delta_to_nanoseconds(ts), ' ns' )
151151 elif not is_timedelta64_object(ts):
152152 raise ValueError (" Invalid type for timedelta "
153- " scalar: %s " % type (ts))
153+ " scalar: {ts_type} " .format( ts_type = type (ts) ))
154154 return ts.astype(' timedelta64[ns]' )
155155
156156
@@ -526,7 +526,7 @@ cdef class _Timedelta(timedelta):
526526 int64_t value # nanoseconds
527527 object freq # frequency reference
528528 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
530530
531531 # higher than np.ndarray and np.matrix
532532 __array_priority__ = 100
@@ -560,9 +560,9 @@ cdef class _Timedelta(timedelta):
560560 return True
561561
562562 # 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__))
566566 if util.is_array(other):
567567 return PyObject_RichCompare(np.array([self ]), other, op)
568568 return PyObject_RichCompare(other, self , reverse_ops[op])
@@ -571,8 +571,9 @@ cdef class _Timedelta(timedelta):
571571 return False
572572 elif op == Py_NE:
573573 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__))
576577
577578 return cmp_scalar(self .value, ots.value, op)
578579
@@ -591,10 +592,6 @@ cdef class _Timedelta(timedelta):
591592
592593 td64_to_tdstruct(self .value, & tds)
593594 self ._d = tds.days
594- if self ._d < 0 :
595- self ._sign = - 1
596- else :
597- self ._sign = 1
598595 self ._h = tds.hrs
599596 self ._m = tds.min
600597 self ._s = tds.sec
@@ -680,60 +677,47 @@ cdef class _Timedelta(timedelta):
680677
681678 Parameters
682679 ----------
683- format : None|all|even_day| sub_day|long
680+ format : None|all|sub_day|long
684681
685682 Returns
686683 -------
687684 converted : string of a Timedelta
688685
689686 """
690- cdef object sign_pretty, sign2_pretty, seconds_pretty, subs
687+ cdef object sign, seconds_pretty, subs, fmt, comp_dict
691688
692689 self ._ensure_components()
693690
694- if self ._sign < 0 :
695- sign_pretty = " -"
696- sign2_pretty = " +"
691+ if self ._d < 0 :
692+ sign = " +"
697693 else :
698- sign_pretty = " "
699- sign2_pretty = " "
694+ sign = " "
700695
701- # show everything
702696 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}"
712699 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)
737721
738722 def __repr__ (self ):
739723 return " Timedelta('{0}')" .format(self ._repr_base(format = ' long' ))
0 commit comments