julia> d = diff(collect(Dates.Date(1980,1,1):Dates.Month(3):Dates.Date(2019,1,1)))
156-element Vector{Day}:
91 days
91 days
92 days
92 days
...
julia> mean(d)
ERROR: InexactError: Int64(91.31410256410257)
julia> d = diff(collect(Dates.Date(1980,1,1):Dates.Month(3):Dates.Date(2019,1,1)))
156-element Vector{Day}:
91 days
91 days
92 days
92 days
...
julia> mean(d)
ERROR: InexactError: Int64(91.31410256410257)
Dates uses an exact integer representation, which is helpful in a lot of cases, but sometimes a floating point representation is more useful. I’ve been using functions like
using Unitful, Dates
"""
seconds(s) -> Unitful.Quantity
Converts a `Dates.TimePeriod` or Unitful time quantity to seconds.
"""
seconds
seconds(s::Unitful.Quantity) = uconvert(u"s", s)
seconds(s::TimePeriod) = seconds(Nanosecond(s))
seconds(s::Nanosecond) = Dates.value(s)*1e-9 * u"s"
as an easy way to convert to one. (With a loop to then also similarly define minutes, hours, days, etc.). It’s been working fine for me, except for that StatsBase.summarystats
is too narrowly typed to handle Unitful quantities.
Perhaps
julia> mean(getfield.(d,:value))
91.31410256410257
You only need to convert the Days to Int64s once when assigning to durs. Afterwards you statistical function calls look about the same.