Opinion:
Some Date comparisons are, in certain contexts, ambiguous. The Julia Dates package refrains from defining operations where these operations are ambiguous. For example:
using Dates
max(Quarter(1), Month(1)) # returns Month(1), as expected
max(Day(1), Month(1)) # throws exception, probably because there exist comparisons, like max(Day(30), Month(1)), which are ambiguous
But why not define
Base.isless(x::Day, y::Year) = x.value < 365*y.value ? true : (x.value > 366*y.value ? false : missing) # type piracy
Alternatively, throw an error instead of returning a missing.
This would be especially useful when people want to take into account frequency.
Why it would be useful:
Throwaway pseudo code, but suppose we have something like
struct TimeSeries{T, STEP} where {T, STEP}
data::AbstractVector
sr::StepRange
end
TimeSeries(av::AbstractVector{T}, sr::StepRange) where T= TimeSeries{T, sr.step}(av, sr)
And we want to conveniently compute covariances between variables at different frequencies. Suppose one is daily and one is monthly. Default fallback might take the lowest frequency step and aggregate/subsample from the higher frequency data. Determining which is lower frequency could be handled by max(ts1.sr.step, ts2.sr.step) – except this is not defined for some pairs of DatePeriod types.
Similarly, suppose you want to be able to subsample a quarterly frequency steprange from a monthly timeseries. That can be accomodated by max. But If you build this syntax want to subsample from Date(Year(2010)):Date(Year(2020)), this defaults to a Day(1) stepsize, which cannot be compared with a month.
As a user, this is not hard to deal with but it is annoying, and it would be obviated by comparisons that give the correct answer when the comparison is unambiguous. I’ve done type piracy to get around this, but type piracy is generally not a good idea.
Thanks!