Read CSV, converting column containing String "Date" to a specific proper Date format

Consider

data = CSV.read(“C:/Stocks/file.csv”)

One column of data holds information about dates in the format shown by, for example, “Feb 1, 2021”.

I wish to convert that entire column of data to the proper date-format m-d-yyyy.

How is that done “on the fly” (i.e., can it be done in the process of creating data)?

Just pass CSV.read the dateformat you want to parse as dates:

julia> CSV.read(IOBuffer("""
           date, value
           "Feb 1, 2021", 12.50
           "Feb 2, 2021", 12.54
           "Feb 3, 2021", 12.76
           """), DataFrame, dateformat="u d, Y")
3×2 DataFrame
 Row │ date         value
     │ Date        Float64
─────┼─────────────────────
   1 │ 2021-02-01    12.5
   2 │ 2021-02-02    12.54
   3 │ 2021-02-03    12.76

Note that they’re now proper Date objects. They print out as YYYY-mm-dd by default, but you can of course format them into a new string however you want.

(watch out, though, I think you have an old version of CSV.jl)

2 Likes