How to convert String type columns of a dataframe to a DateTime type and a Float64 type, using either plain DataFrames.jl or Query.jl

In plain DataFrames.jl you would write this transformation as:

julia> transform(df_0, :Inicio => ByRow(x -> DateTime(x, DateFormat("d u Y  H:M "))), :Q1 => ByRow(x -> parse(Float64, x)), renamecols=false)
3×2 DataFrame
 Row │ Inicio               Q1
     │ DateTime             Float64
─────┼──────────────────────────────
   1 │ 2021-01-10T15:00:00      0.5
   2 │ 2021-01-10T16:43:00      0.0
   3 │ 2021-01-10T17:13:00      0.5

but this is a bit verbose.

You could also just do:

julia> DataFrame(Inicio=DateTime.(df_0.Inicio, DateFormat("d u Y  H:M ")), Q1=parse.(Float64, df_0.Q1))
3×2 DataFrame
 Row │ Inicio               Q1
     │ DateTime             Float64
─────┼──────────────────────────────
   1 │ 2021-01-10T15:00:00      0.5
   2 │ 2021-01-10T16:43:00      0.0
   3 │ 2021-01-10T17:13:00      0.5

which is shorter (but would create only two columns, while transform would keep all other columns if needed).

Also, if it is OK for you to work in-place you can just write:

df_0.Inicio = DateTime.(df_0.Inicio, DateFormat("d u Y  H:M "))
df_0.Q1 = parse.(Float64, df_0.Q1))

which should be quite readable.

3 Likes