title: Format timedelta64 string output
body: <p>In a similar vein to this <a href="https://stackoverflow.com/questions/18433142/string-formatting-of-timedeltas-in-pandas">question</a>, I have a <code>numpy.timedelta64</code> column in a pandas DataFrame. As per this <a href="https://stackoverflow.com/a/18433197/677122">answer</a> to the aforementioned question, there is a function <code>pandas.tslib.repr_timedelta64</code> which nicely displays a timedelta in days, hours:minutes:seconds. I would like to format them only in days and hours.</p>
  
  <p>So what I've got is the following:</p>
  
  <pre><code>def silly_format(hours):
      (days, hours) = divmod(hours, 24)
      if days &gt; 0 and hours &gt; 0:
          str_time = "{0:.0f} d, {1:.0f} h".format(days, hours)
      elif days &gt; 0:
          str_time = "{0:.0f} d".format(days)
      else:
          str_time = "{0:.0f} h".format(hours)
      return str_time
  
  df["time"].astype("timedelta64[h]").map(silly_format)
  </code></pre>
  
  <p>which gets me the desired output but I was wondering whether there is a function in <code>numpy</code> or <code>pandas</code> similar to <code>datetime.strftime</code> that can format <code>numpy.timedelta64</code> according to some format string provided?</p>
  
  <hr>
  
  <p>I tried to adapt @Jeff's solution further but it is way slower than my answer. Here it is:</p>
  
  <pre><code>days = time_delta.astype("timedelta64[D]").astype(int)
  hours = time_delta.astype("timedelta64[h]").astype(int) % 24
  result = days.astype(str)
  mask = (days &gt; 0) &amp; (hours &gt; 0)
  result[mask] = days.astype(str) + ' d, ' + hours.astype(str) + ' h'
  result[(hours &gt; 0) &amp; ~mask] = hours.astype(str) + ' h'
  result[(days &gt; 0) &amp; ~mask] = days.astype(str) + ' d'
  </code></pre>