Reputation: 45
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
Reputation: 42194
The DataFrame
's column identifiers are Symbol
s 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 String
s 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