Reputation: 45
I am trying to read from file a datetime string followed by some data. It provide error. I reduced it to following task which produces error
import numpy as np
from matplotlib.dates import strpdate2num
from io import StringIO
d = StringIO(u'16-03-13 20:13:55')
date = np.loadtxt(d, converters={0:strpdate2num('%y-%m-%d %H:%M:%S')})
date
provides following error:
ValueError: time data '16-03-13' does not match format '%y-%m-%d %H:%M:%S'
execution of separate parts works well
d = StringIO(u'16-03-13')
date = np.loadtxt(d, converters={0:strpdate2num('%y-%m-%d')})
date
gives me
array(736036.0)
the piece alone with time works also well
d = StringIO(u"20:13:55")
date= np.loadtxt(d, delimiter=' ', unpack=True, converters={0: strpdate2num('%H:%M:%S')})
date
gives
array(693596.8429976852)
What I am doing wrong?
Upvotes: 0
Views: 1578
Reputation: 69136
The problem is because np.loadtxt
is trying to split your string up into two components, as its default delimiter is a space and you have a space in your string.
If you change the delimiter to anything else, this will work, for example:
date = np.loadtxt(d, converters={0:strpdate2num('%y-%m-%d %H:%M:%S')}, delimiter=',')
print date
# 736036.842998
Of course, if you have data after your string, and your delimiter is a space between the datetime string and the data, you may need to read the date and time as two separate strings, then join them together after.
Upvotes: 1