1

In pandas I have a TimedeltaIndex named durations

print(durations)

TimedeltaIndex(['05:00:00', '11:15:00', '07:30:00'], dtype='timedelta64[ns]', name='Date', freq=None)

Is it possible just to print each element of the TimedeltaIndex individually?

For example if I print durations[0] this will output: Timedelta('0 days 05:00:00')

What would I need to print just 05:00:00 without any associated Timedelta information?

EDIT possible solution? Create empty list..

stuff = []
stuff = durations.index.astype(str).str[-8:-3]

Now it seems like I can print each element of the list:

stuff[0]
stuff[1]

returns:

'00:00'
'05:00'
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • So it's a little ambiguous. `timedelta` can store any unit of time; that display is only valid when you have times between `[0, 24Hours)`. Once you add a large timedelta like '25:01:15'` the display now shows as `'0 days 05:00:00'` for all of the timedeltas. The same issue arises with negative timedeltas... It's really difficult to know how to handle these cases. – ALollz May 06 '20 at 15:59
  • There's are numerous proposed solutions: https://stackoverflow.com/questions/538666/format-timedelta-to-string. Many of the highly voted ones will fail with large or negative timedeltas so read carefully – ALollz May 06 '20 at 16:10
  • I made an edit to the post, does this seem like a wonky way of creating a solution? – bbartling May 06 '20 at 16:14

1 Answers1

1

You can use split to separate the different parts of the output and only choose the third item like below:

print(str(durations[0]).split(' ')[2])
CHRD
  • 1,917
  • 1
  • 15
  • 38