Skip to content

Commit

Permalink
Making changes to getindex.jl to resolve the issue xKDR#190, xKDR#194
Browse files Browse the repository at this point in the history
  • Loading branch information
siddjain444 committed Apr 25, 2024
1 parent 449b42c commit 986af3a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/getindex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ julia> ts[1, "x1"]; # same as above
###

### Inputs: row scalar, column scalar; Output: scalar

function Base.getindex(ts::TSFrame, i::Int, j::Int)
return ts.coredata[i,j+1]
end
Expand All @@ -273,7 +274,7 @@ end

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


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


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


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


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


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


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


function Base.getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{AbstractString, Symbol}}
if length(unique(map(x -> typeof(x), j))) == 1
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
if length(unique(map(x -> typeof(x), j))) == 1
ts[idx, j]
else
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
end
end

end
###

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


function Base.getindex(ts::TSFrame, i::UnitRange, j::Union{AbstractString, Symbol})
ts[collect(i), j]
end
Expand All @@ -395,6 +404,7 @@ function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{Int})
ts[collect(i), j]
end


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


function Base.getindex(ts::TSFrame, ::Colon, j::Union{AbstractString, Symbol})
ts[1:TSFrames.nrow(ts), j]
end
Expand Down

0 comments on commit 986af3a

Please sign in to comment.