Skip to content

Split example from documentation fails  #527

Open
@willcoxe

Description

@willcoxe

Hi,

I am attempting to split TimeArrays by date and apparently it's not happening. I get an error. My process is as follows:

Exactly as the example except 2018 is changed to 2019 for the second date:

julia> data = (datetime = [DateTime(2018, 11, 21, 12, 0), DateTime(2019, 11, 21, 13, 0)],
col1 = [10.2, 11.2],
col2 = [20.2, 21.2],
col3 = [30.2, 31.2])

Then I create the TimeArray

julia> ta = TimeArray(data; timestamp = :datetime, meta = "Example")
2×3 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2018-11-21T12:00:00 to 2019-11-21T13:00:00
┌─────────────────────┬──────┬──────┬──────┐
│ │ col1 │ col2 │ col3 │
├─────────────────────┼──────┼──────┼──────┤
│ 2018-11-21T12:00:00 │ 10.2 │ 20.2 │ 30.2 │
│ 2019-11-21T13:00:00 │ 11.2 │ 21.2 │ 31.2 │
└─────────────────────┴──────┴──────┴──────┘

Then I want to split by date using from and it doesn't work:

julia> from(ta, Date(2019, 10, 10))
ERROR: MethodError: no method matching from(::TimeArray{Float64, 2, DateTime, Matrix{Float64}}, ::Date)

If I run the code manually, it works without issue, so:

julia> d = Date(2019, 10, 10)
2019-10-10

julia> length(ta) == 0 ? ta :
d < timestamp(ta)[1] ? ta :
d > timestamp(ta)[end] ? ta[1:0] :
ta[searchsortedfirst(timestamp(ta), d):end]
1×3 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2019-11-21T13:00:00 to 2019-11-21T13:00:00
┌─────────────────────┬──────┬──────┬──────┐
│ │ col1 │ col2 │ col3 │
├─────────────────────┼──────┼──────┼──────┤
│ 2019-11-21T13:00:00 │ 11.2 │ 21.2 │ 31.2 │
└─────────────────────┴──────┴──────┴──────┘

My julia version is 1.10.4

If I edit the function per:

tst(ta::TimeArray{T}, d::D) where {T,D} =
length(ta) == 0 ? ta :
d < timestamp(ta)[1] ? ta :
d > timestamp(ta)[end] ? ta[1:0] :
ta[searchsortedfirst(timestamp(ta), d):end]

Then it works:

tst(ta, Date(2019, 10, 10))
1×3 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2019-11-21T13:00:00 to 2019-11-21T13:00:00
┌─────────────────────┬──────┬──────┬──────┐
│ │ col1 │ col2 │ col3 │
├─────────────────────┼──────┼──────┼──────┤
│ 2019-11-21T13:00:00 │ 11.2 │ 21.2 │ 31.2 │
└─────────────────────┴──────┴──────┴──────┘

Should I be doing something differently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions