Converting columns in a DataFrame from String to Date (with a specific format)

I am trying to convert a DataFrame column populated with dates whose type is String and format DD/MM/YYYY to dates with Date type and format YYYYMMDD

Tried A[!,:DATE] = parse.(Date, A[!,:DATE])

But it errors, with: ArgumentError: Unable to parse date time. Expected directive Delim(-)

Which would be the correct way to achieve this conversion? Thank you!

Parsing into a Date is done by:

julia> d1="02/02/2022"
"02/02/2022"

julia> date=Date(d1,"dd/mm/yyyy")
2022-02-02

julia> typeof(date)
Date

Output (as String) is done by:

julia> s=Dates.format(date,"Ymmdd")
"20220202"

So:

A[!,:DATE]= Dates.format.(Date.( A[!,:DATE],"dd/mm/yyyy"),"Ymmdd")

Typically, when it’s about DataFrames, after a while, much more elegant versions are popping up, just wait for it…

2 Likes

Thank you very much!

How did you get date strings into a data frame in the first place?

In case that you use CSV.jl, you can specify a date format when reading the file Reading · CSV.jl

2 Likes

Don’t call Dates.format on your column, because it converts Dates to String so in general it is a bad practice. Leave it as Date unless you want to present data.

using Dates
f(x; df = dateformat"m/d/y") = Date(x, df)
transform(df, :date=>ByRow(f))