Convert string "date code" for month and year to proper date type

I have a data frame with where one of the columns contains a string “date code”. An example of this code is as follows:


which indicates the 4th month of the year 2022.


would indicate the 11th month of 2023.

Rather than working with this code in string, I would like to convert the column of string code to a column of proper date types. How can that be done?

julia> dStr = "04_22"

julia> m, y = split(dStr, '_')
2-element Vector{SubString{String}}:

julia> DateTime(2000 + parse(Int, y), parse(Int, m), 1)


vd = ["04_22","11_23"]
vd .= replace.(vd, "_" => "-20")
Date.(vd, "mm-yyyy")

There is no need to use split/parse/replace here, just parse directly with a DateFormat (and then add 2000 years):

julia> Date("11_23", dateformat"mm_yy") + Year(2000)

That is nice. Would you know why the dot is not needed to broadcast the sum of the 2000 years with the vector?

vd = ["04_22","11_23"]
Date.(vd, "mm_yy") .+ Year(2000)

Yes, that is not intended, but something that was forgotten to deprecate for Julia 1.0, see Dates/src/deprecated.jl.


That is very nice and intuitive. Suppose I knew that it was not the first of the month that the code indicated, but always the last day of the month, what would your solution look like then?

Maybe you could do:

Date.(vd, "mm_yy") .+ Year(2000) .+ Month(1) .- Day(1)
1 Like

An alternative for the last day of the month is

@. Dates.lastdayofmonth(Date(vd, "mm_yy") + Year(2000))
1 Like