codedancer
codedancer

Reputation: 1634

Convert string and int64 into date type in Julia

I am trying to convert String and Int64 column types into date type Julia. I tried a number of ways like Dates(). What's the best way to do this by column?

 10×2 DataFrame
 Row │ stock_date   price   
     │ String      Float64 
─────┼─────────────────────
   1 │ Jan 1 2000    39.81
   2 │ Feb 1 2000    36.35
   3 │ Mar 1 2000    43.22
   4 │ Apr 1 2000    28.37
   5 │ May 1 2000    25.45
   6 │ Jun 1 2000    32.54
   7 │ Jul 1 2000    28.4
   8 │ Aug 1 2000    28.4
   9 │ Sep 1 2000    24.53
  10 │ Oct 1 2000    28.02

 10×2 DataFrame
 Row │ Year   ma_value 
     │ Int64  Float64  
─────┼─────────────────
   1 │  1960   12.9945
   2 │  1961   12.4031
   3 │  1962   13.943
   4 │  1963   13.0059
   5 │  1964   14.9382
   6 │  1965   13.2202
   7 │  1966   12.9324
   8 │  1967   12.8837
   9 │  1968   12.2977
  10 │  1969   11.9549

Upvotes: 3

Views: 646

Answers (1)

natemcintosh
natemcintosh

Reputation: 870

For the year column, we can use the simplest form of the Date constructor and broadcast it over the column, e.g.

using Dates

DateTime.(my_df.Year)

will create dates of the year.

For your other column which already has some formatting, we can use the method with args DateTime.(my_df.stock_date, "u d Y"). For reference on what letters to use in the format string, see https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.format-Tuple{TimeType,%20AbstractString}

Upvotes: 3

Related Questions