Converting DateTime in DataFrames indo Data

Hi! I’m reading csv file and puttining it into DataFrames. I can’t cope with changing the time column from “2019-11-18T13:09:31Z” format to “2019-11-18”. Can someone help me ?

I have code

function read_data('query.csv')
    cap_file  = readlines(datadir('query.csv'))
    df = DataFrame(data(cap_file[end]))
    df = @transform(df, time = string.(chop.(:time)))
    @transform(df, time = Date.(:time))
end

but it gives me “syntax: character literal contains multiple characters”

This should help

julia> s = "2019-11-18T13:09:31Z"
"2019-11-18T13:09:31Z"

julia> parsed_datetime = DateTime(s, dateformat"y-m-dTH:M:SZ")
2019-11-18T13:09:31

julia> d = Date(parsed_datetime)
2019-11-18

Why not just

Date(s[1:10])

You could do that as well. But sub-setting a string like that can make code less readable and more fragile.

Sorry, but I still have problem. How to do it on one of the columns of a table from DataFrames, where we have thousands of such dates?

Are the dates all in the same format?

yes

Great, then you can just do something like

@transform(time = Date.(DateTime.(:time, dateformat"y-m-dTH:M:SZ")))
1 Like

Thank you :star_struck:

A great alternative is CSV.File. It can parse the timestamps for you.

CSV.File

D = DataFrame!(CSV.File(raw"C:\Data.csv",
		datarow=3, # Data starts on the third row
		dateformat=dateformat"y-m-dTH:M:SZ",
		delim=',',
		header=["Timestamp","Temperature","Diagnostic","Count"],
		types=[DateTime,Float64,String,Int64]))
1 Like