I would like to read CSV files with a time column where the format of the time can be one of several alternatives. CSV.file() does not automatically recognize the formats.
Now I do like follows but is there are better way?
using Dates, DataFrames, CSV
formats = ["yyyy-mm-dd HH:MM:SS", "yyyy-mm-ddTHH:MM:SS"]
# read file
cf = DataFrame(CSV.File(filename, missingstring = "NA", types = Dict(:time => String)))
# convert time column
for fmt in formats
try
cf.time = DateTime.(cf.time, DateFormat(fmt))
break # if success, exit the loop
catch e
if isa(e, ArgumentError) || isa(e, MethodError)
continue
else
rethrow(e) # Re-throw the exception if it's not the type I expected
end
end
end