-4

I want to convert time offsets formatted as str to datetime.timedelta objects, then plot the curve of observations versus time.

'5 days, 4:23:52.480000'  ;      17.647166
'5 days, 5:56:09.166000' ;        22.916071
'5 days, 8:21:40.261000'  ;      18.922329
'5 days, 9:53:18.070000'    ;    21.392157
'6 days, 0:07:54.487000'  ;      20.275597
Paul
  • 10,381
  • 13
  • 48
  • 86
Nabil
  • 1
  • 1

2 Answers2

0

There is no parser for timedeltas. So you have to build one yourself:

import re
delta = '5 days, 4:23:52.480000' 
days, hours, minutes, seconds = re.match('(?:(\d+) days, )?(\d+):(\d+):([.\d+]+)', delta).groups()
total_seconds = ((int(days or 0) * 24 + int(hours)) * 60 + int(minutes)) * 60 + float(seconds)
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

While I don't know of anything that parses text into timedelta, the format you have specified is understood by the pandas.Timedelta constructor, so if you want, you can use that to construct your timedeltas:

import pandas as pd
import matplotlib.pyplot as plt

x = [['5 days, 4:23:52.480000', 17.647166],
     ['5 days, 5:56:09.166000', 22.916071],
     ['5 days, 8:21:40.261000', 18.922329],
     ['5 days, 9:53:18.070000', 21.392157],
     ['6 days, 0:07:54.487000', 20.275597]]

df = pd.DataFrame(x, columns=['TimeOffset', 'Value'])

df['TimeOffset'] = df['TimeOffset'].apply(pd.Timedelta)

plt.plot(df['TimeOffset'], df['Value'])
plt.show()

People who know pandas better, feel free to edit this answer (and remove this message) - I feel like the way I constructed the pd.Timedelta column may be non-idiomatic.

Paul
  • 10,381
  • 13
  • 48
  • 86