I read through the docs on the Dates Module, but I can’t figure out how to convert a Day
to an Int
. I have an array of {Day,1}
, and want to perform mean
and std
and so on. Also, for Plotting they seem to be treated more as a categorical variable than continuous one.
I think you’re looking for Dates.value
:
help?> Dates.value
Dates.value(x::Period) → Int64For a given period, return the value associated with that period. For example, value(Millisecond(10)) returns 10 as an integer.
For example:
julia> using Dates
julia> d = Day(42)
42 days
julia> Dates.value(d)
42
5 Likes
That’s the ticket!! Thanks.
what about an Array? For example:
julia> n_day
4-element Array{Day,1}:
19 days
25 days
19 days
25 days
julia> Dates.values(n_day)
4-element Array{Day,1}:
19 days
25 days
19 days
25 days
You can use broadcasting notation to efficiently map a function to all elements of a vector:
julia> using Dates
julia> n_day = Day.(rand(1:31, 5))
5-element Array{Day,1}:
19 days
23 days
27 days
19 days
15 days
julia> Dates.value.(n_day)
5-element Array{Int64,1}:
19
23
27
19
15
2 Likes
thanks!
broadcasting notation . follows the function Dates.value
got it