Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split example from documentation fails #527

Open
willcoxe opened this issue Jul 12, 2024 · 2 comments
Open

Split example from documentation fails #527

willcoxe opened this issue Jul 12, 2024 · 2 comments

Comments

@willcoxe
Copy link

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?

@iblislin
Copy link
Collaborator

I think it's type issue

the time array type is
TimeArray{Float64, 2, DateTime, Matrix{Float64}}

then the function signature requires that

from(::TimeArray{Float64, 2, DateTime, Matrix{Float64}}, ::DateTime)
vs the error message:
from(::TimeArray{Float64, 2, DateTime, Matrix{Float64}}, ::Date)

@willcoxe
Copy link
Author

Thank you, and yes, this is the case, the following works perfectly:
from(ta, DateTime(Date(2019, 10, 10)))

It would be helpful if this was shown in the documentation @

https://juliastats.org/TimeSeries.jl/dev/split/#from
https://juliastats.org/TimeSeries.jl/stable/split/#from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants