I have a file with entries that look like
2013-12-11 23:00:27.003293,$PAMWV,291,R,005.8,M,A*36
2013-12-11 23:00:28.000295,$PAMWV,284,R,005.5,M,A*3F
2013-12-11 23:00:29.000295,$PAMWV,273,R,004.0,M,A*33
2013-12-11 23:00:30.003310,$PAMWV,007,R,004.9,M,A*3B
Considering the delimiters are actually a comma (','), this is a classic CSV file.
I've tried:
wind = loadtxt("/disk2/Wind/ws425.log.test", dtype(str,float), delimiter=',', usecols=(0,4))
ts= time.strptime(str(wind[:,0]), '%Y-%m-%d %H:%M:%S.%f')
and what I get is
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-484b71dea724> in <module>()
----> 1 ts= time.strptime(str(wind[:,0]), '%Y-%m-%d %H:%M:%S.%f')
/opt/Enthought/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/_strptime.pyc in _strptime_time(data_string, format)
452
453 def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
--> 454 return _strptime(data_string, format)[0]
/opt/Enthought/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
323 if not found:
324 raise ValueError("time data %r does not match format %r" %
--> 325 (data_string, format))
326 if len(data_string) != found.end():
327 raise ValueError("unconverted data remains: %s" %
ValueError: time data "['2013-12-12 00:00:02.251311' '2013-12-12 00:00:03.255296'\n '2013-12-12 00:00:04.254294' ..., '2013-12-12 16:10:50.579022'\n '2013-12-12 16:10:51.607035' '2013-12-12 16:10:52.604020']" does not match format '%Y-%m-%d %H:%M:%S.%f'
I suspect I'm mis-using the data type assignment in time.strptime() but I've been unsuccessful in finding a correct type so far.
Suggestions?
Oh the real problem here is that time.strptime does not support %f for microseconds, see here for a list of formatting characters supported by time.strptime and time.strftime.
What you do want is datetime.strptime which does support the %f formatting character for microseconds.
I had to do something like
For time series data, though, I've switched to using
pandas
, because it makes a lot of things much easier:time.strptime()
expects a string such as'2013-12-11 23:00:30.003310'
but you are giving it a string representation of an array instead:The minimal fix is to parse one item at a time:
Or you could use
converters
parameter forloadtxt
:Output
You just have some errors in your NumPy
loadtxt
call where you define thedtype
. It should bedtype=[('date', 'str', 26), ('wind', 'float')]
; you must specify the size of the string. Now you can reference the date field using its name, EG:wind['date']
. Yourstrptime
format is fine, but you want thedatetime
module from Python'sdatetime
package, nottime
.This returns the following:
Maybe you want to feed that back into your NumPy array?
this yields
I am not sure what is wrong with numpy; but with csv it works great: