user3856487
user3856487

Reputation: 45

Julia date as column name

I am building a data frame and I wish to use Julia's Dates as the name of a column, is that possible?

that is if I have a dataframe

df = DataFrame(); df.DateTime(2013)=2:4

which returns an error.

Upvotes: 2

Views: 103

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42194

The DataFrame's column identifiers are Symbols so you need to convert DateTime to a Symbol:

julia> df = DataFrame();

julia> df[!, Symbol(DateTime(2013))] = 2:4;

julia> df
3×1 DataFrame
│ Row │ 2013-01-01T00:00:00 │
│     │ Int64               │
├─────┼─────────────────────┤
│ 1   │ 2                   │
│ 2   │ 3                   │
│ 3   │ 4                   │

This will also work with Strings with the same result:

julia> df[!, string(DateTime(2014))] = 12:14;

julia> df
3×2 DataFrame
│ Row │ 2013-01-01T00:00:00 │ 2014-01-01T00:00:00 │
│     │ Int64               │ Int64               │
├─────┼─────────────────────┼─────────────────────┤
│ 1   │ 2                   │ 12                  │
│ 2   │ 3                   │ 13                  │
│ 3   │ 4                   │ 14                  │

Finally note that df[:colname] syntax is depreciated in favor of df[!, :colname] but in order to find it out you need to start Julia with:

julia --depwarn=yes

Upvotes: 3

Related Questions