python - Get Numpy genfromtxt converter to use def function -
i have searched through forums , cannot seem work out following problem. new python little programming experience issue might trivial.
the want convert date-time string date-time format using date.strptime
classmethod.
the problem string format within column not consistent (majority %y-%m-%d $h:$m:$s.%f
) ; when time falls on second, millisecond decimals omitted (format should instead %y-%m-%d $h:$m:$s
). when strptime
encounters unrecognized format, place none
value in array element.
is there way create exception in lambda
function (i.e., valueerror
exception), if not, how pass string value "normal" def timeconv(x)
function genfromtxt
converter option?
maybe there better way of approaching problem...?
my current code results in none
value when format %y-%m-%d $h:$m:$s
:
timeconv = lambda x: datetime.strptime(x, '\"%y-%m-%d $h:$m:$s.%f\"') time = np.genfromtxt(file, dtype='object', delimiter=',', skip_header=4, usecols=(0), converters = {0: timeconv})
you use try..except
first try 1 format, , if not work, catch exception , try other format:
import datetime dt import numpy np def timeconv(x): try: return dt.datetime.strptime(x, '%y-%m-%d %h:%m:%s.%f') except valueerror err: return dt.datetime.strptime(x, '%y-%m-%d %h:%m:%s') time = np.genfromtxt(file, dtype='object', delimiter=',', skip_header=4, usecols=(0), converters = {0: timeconv})
the function, timeconv
passed genfromtxt
same way passed lambda
.
the dateutil module has date string parser not require specify exact format of date string. using dateutil write
import dateutil.parser dparser import numpy np time = np.genfromtxt(file, dtype='object', delimiter=',', skip_header=4, usecols=(0), converters = {0: dparser.parse})
note although dparser.parse
easy use, there ambiguous date strings 2013-8-9
(august 8 or sept 9?) require more care. sure read on dayfirst
, yearfirst
parameters can control parser's behavior.
Comments
Post a Comment