Search code examples
julia

How to create a vector of dates


I would like to be able to create a vector of dates given a start and and end date in a dataframe. However, I receive the following error: "Cannot convert an object of type Int64 to an object of type Day"

using DataFrames, Tidier, TidierDates

df = DataFrame(date_start = "2019-01-01", date_end = "2019-01-31")
describe(df)
df[!, :date_start] = TidierDates.ymd.(df[!, :date_start])
df[!, :date_end] = TidierDates.ymd.(df[!, :date_end])
describe(df)

df[1,:date_start]:df[1,:date_end]

I thought something like this might work, but no:

date_vec = Dates.value(df[1,:date_start]):1:Dates.value(df[1,:date_end]) |> collect
date_days = Dates.Day(date_vec)

Solution

  • Working on an extended DataFrame to show the row-wise operation.

    using DataFrames
    using Dates
    
    res = collect.(range.(Date.(df.date_start), Date.(df.date_end)));
    
    res[1]
    31-element Vector{Date}:
     2019-01-01
     2019-01-02
    ...
     2019-01-30
     2019-01-31
    
    res[2]
    9-element Vector{Date}:
     2019-01-03
     2019-01-04
    ...
     2019-01-10
     2019-01-11
    

    extended Data

    df = DataFrame(["2019-01-01" "2019-01-31"; 
                    "2019-01-03" "2019-01-11"], ["date_start", "date_end"])
    
    2×2 DataFrame
     Row │ date_start  date_end
         │ String      String
    ─────┼────────────────────────
       1 │ 2019-01-01  2019-01-31
       2 │ 2019-01-03  2019-01-11