Julia: [Dates] size and length not defined for DateTime

Created on 29 Apr 2019  路  8Comments  路  Source: JuliaLang/julia

size(Dates.DateTime(2016, 04, 10))
ERROR: MethodError: no method matching size(::DateTime)
should yield
()

length(Dates.DateTime(2016, 04, 10))
ERROR: MethodError: no method matching length(::DateTime)
should yield
1

Most helpful comment

You could also check date in holidays which doesn't need to construct a temporary boolean array.

All 8 comments

Why would you expect these to be defined?
What utility do you wish to get out of having this?

Things that are not iterable do not normally define these (infact not even all of them define them few more rules around that)
Note that Numbers are iterable, which may have mislead you.

For example Strings do not define size (though they do length)

julia> size("some string")
ERROR: MethodError: no method matching size(::String)

Coming from MATLAB I wrote a function that checks something on a vector of DateTime and expected it to work with a single DateTime. But it does not.
The workaround I found is to dispatch on Array{DateTime} or DateTime and have two functions.

The actual problem was that indexin(DateTime, Array{DateTime}) does not work because length(DateTime) is not defined.

What does the function that operates on a vector of DateTime do?

It checks what instances are on a holiday. The possible holidays are given by a vector of dates.

using Dates
isholiday(date::Date, holidays::Vector{Date}) = any(date .== holidays)
isholiday(datetime::DateTime, holidays::Vector{Date}) = isholiday(Date(datetime), holidays)

You could also check date in holidays which doesn't need to construct a temporary boolean array.

Would such functionality be worthy to include in the Dates module?
Like: https://docs.scipy.org/doc/numpy/reference/generated/numpy.is_busday.html

We have packages for this. E.g. https://github.com/JuliaFinance/BusinessDays.jl

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manor picture manor  路  3Comments

omus picture omus  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments

ararslan picture ararslan  路  3Comments

sbromberger picture sbromberger  路  3Comments