Skip to content

Commit 449b42c

Browse files
committed
Updating the getindex.jl to address the issue xKDR#194 and xKDR#190
1 parent 56440c4 commit 449b42c

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

src/apply.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ julia> show(ts_weekly[1:10])
132132
```
133133
"""
134134
function apply(ts::TSFrame, period::T, fun::V, index_at::Function=first; renamecols::Bool=true) where {T<:Dates.Period, V<:Function}
135+
if !issorted(ts[:, :Index])
136+
throw(ArgumentError("The `Index` column in the TSFrame object is not sorted."))
137+
end
138+
135139
ep = endpoints(ts, period)
136140

137141
j = 1

src/diff.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ julia> diff(ts, 3)[1:10] # difference over the third row
7373
```
7474
"""
7575

76-
# Diff
76+
# Diff, Here we have used ArgumentError and not DomainError
77+
7778
function diff(ts::TSFrame, periods::Int = 1)
7879
if periods <= 0
79-
error("periods must be a postive int")
80+
#error("periods must be a postive int")
81+
throw(ArgumentError("`periods` must be a postive integer."))
8082
end
83+
8184
ddf = ts.coredata[:, Not(:Index)] .- TSFrames.lag(ts, periods).coredata[:, Not(:Index)]
8285
insertcols!(ddf, 1, "Index" => ts.coredata[:, :Index])
8386
TSFrame(ddf, :Index)
84-
end
87+
end

src/endpoints.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ julia> datetimesecondsrandom[endpoints(datetimesecondsrandom, Hour(1))]
280280
"""
281281
function endpoints(values::AbstractVector, on::Function, k::Int=1)
282282
if (k <= 0)
283-
throw(DomainError("`k` needs to be greater than 0"))
283+
# throw(DomainError("`k` needs to be greater than 0"))
284+
throw(ArgumentError("`k` must be a postive integer."))
284285
end
285286

286287
ex = Expr(:call, on, values)
@@ -304,7 +305,20 @@ function endpoints(ts::TSFrame, on::Function, k::Int=1)
304305
endpoints(index(ts), on, k)
305306
end
306307

307-
function endpoints(timestamps::AbstractVector{T}, on::V)::Vector{Int} where {T<:Union{Date, DateTime, Time}, V<:Dates.Period}
308+
function endpoints(timestamps::AbstractVector{T}, on::V)::Vector{Int} where {T<:Union{Date, DateTime, Time},
309+
V<:Union{
310+
Year,
311+
Quarter,
312+
Month,
313+
Week,
314+
Day,
315+
Hour,
316+
Minute,
317+
Second,
318+
Millisecond,
319+
Microsecond,
320+
Nanosecond
321+
}}
308322
if (on.value <= 0)
309323
throw(DomainError("`on.value` needs to be greater than 0"))
310324
end

src/getindex.jl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ julia> ts[1, "x1"]; # same as above
252252
###
253253

254254
### Inputs: row scalar, column scalar; Output: scalar
255-
256255
function Base.getindex(ts::TSFrame, i::Int, j::Int)
257256
return ts.coredata[i,j+1]
258257
end
@@ -274,7 +273,7 @@ end
274273

275274
### Inputs: row scalar, column vector; Output: TSFrame
276275
function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{Int})
277-
TSFrame(ts.coredata[[i], Cols(:Index, j.+1)]) # increment: account for Index
276+
TSFrame(ts.coredata[[i], Cols(:Index, j.+1)]) # increment: account for Index
278277
end
279278

280279

@@ -286,7 +285,6 @@ function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{T}) where {T<:Unio
286285
end
287286
end
288287

289-
290288
function Base.getindex(ts::TSFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType}
291289
idx = findfirst(x -> x == dt, index(ts))
292290
ts[idx, j]
@@ -314,7 +312,6 @@ function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Int)
314312
ts.coredata[i, j+1] # increment: account for Index
315313
end
316314

317-
318315
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Union{AbstractString, Symbol})
319316
ts.coredata[i, j]
320317
end
@@ -327,7 +324,6 @@ function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Int) where {T<:Tim
327324
ts[idx, j]
328325
end
329326

330-
331327
function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Union{AbstractString, Symbol}) where {T<:TimeType}
332328
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
333329
if length(idx) == 0
@@ -347,7 +343,6 @@ function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{In
347343
TSFrame(ts.coredata[i, Cols(:Index, j.+1)]) # increment: account for Index
348344
end
349345

350-
351346
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
352347
if length(unique(map(x -> typeof(x), j))) == 1
353348
TSFrame(ts.coredata[i, Cols(:Index, j)])
@@ -356,7 +351,6 @@ function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}
356351
end
357352
end
358353

359-
360354
function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::AbstractVector{Int}) where {T<:TimeType}
361355
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
362356
if length(idx) == 0
@@ -365,15 +359,13 @@ function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::AbstractVector{Int
365359
ts[idx, j]
366360
end
367361

368-
369362
function Base.getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{AbstractString, Symbol}}
370-
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
371363
if length(unique(map(x -> typeof(x), j))) == 1
364+
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
372365
ts[idx, j]
373366
else
374367
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
375-
end
376-
368+
end
377369
end
378370
###
379371

@@ -393,7 +385,6 @@ function Base.getindex(ts::TSFrame, i::UnitRange, j::Int)
393385
ts[collect(i), j]
394386
end
395387

396-
397388
function Base.getindex(ts::TSFrame, i::UnitRange, j::Union{AbstractString, Symbol})
398389
ts[collect(i), j]
399390
end
@@ -404,7 +395,6 @@ function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{Int})
404395
ts[collect(i), j]
405396
end
406397

407-
408398
function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
409399
if length(unique(map(x -> typeof(x), j))) == 1
410400
ts[collect(i), j]
@@ -514,7 +504,6 @@ function Base.getindex(ts::TSFrame, ::Colon, j::Int)
514504
ts[1:TSFrames.nrow(ts), j]
515505
end
516506

517-
518507
function Base.getindex(ts::TSFrame, ::Colon, j::Union{AbstractString, Symbol})
519508
ts[1:TSFrames.nrow(ts), j]
520509
end

src/subset.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}}
66
```
77
8-
Create a subset of `ts` based on the `Index` starting `from`
8+
Create a subset of `ts` based on the [`Index`](@ref) starting `from`
99
(inclusive) till `to` (inclusive).
1010
1111
# Examples
@@ -139,6 +139,7 @@ julia> subset(ts,Date("2022-9-27"),:)
139139
140140
"""
141141
function subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}}
142+
#TSFrame(DataFrames.subset(ts.coredata, :Index => x -> from .<= x .<= to))
142143
TSFrame(DataFrames.subset(ts.coredata, :Index => x -> from .<= x .<= to))
143144
end
144145

src/utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ function _check_consistency(ts::TSFrame)::Bool
537537
issorted(index(ts))
538538
end
539539

540+
540541
"""
541542
542543
```julia

0 commit comments

Comments
 (0)