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"

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

julia> typeof(date)

Output (as String) is done by:

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


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…


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


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))