0

I have some time data that I read into a numpy array as a string. It's in the format

x = ['11:13:01.337 AM', '11:13:03.337 AM', '11:13:05.337 AM']

I want to be able to plot this cleanly over an indefinite amount of time, let's say the data goes for 4 hours. How can I convert this into a reasonable form to plot? I am not sure what form would be best so suggestions would be helpful. As you can probably tell I'm a noob, my boss and I appreciate the help!

1 Answers1

0

Unlike time.strptime, datetime.strptime can parse such strings, including microseconds, and you can convert the result to a number (seconds since midnight):

import datetime
def parse(s):
    d = datetime.datetime.strptime(s, "%I:%M:%S.%f %p")
    return 3600 * d.hour + 60 * d.minute + d.second + d.microsecond * 1e-6

And use it like this:

x = ['11:13:01.337 AM', '11:13:03.337 AM', '11:13:05.337 AM']
print([parse(s) for s in x])

This should be safe even in the presence of DST changes because strptime merely parses the time, and does not attempt any such semantic conversions

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92