Skip to content

Commit 0bc8081

Browse files
committed
Updating the getindex function to throw argument error when string and symbol are passed simultaneously as input to the function
1 parent d1ed39e commit 0bc8081

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

src/getindex.jl

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ julia> ts[1, "x1"]; # same as above
252252
###
253253

254254
### Inputs: row scalar, column scalar; Output: scalar
255+
255256
function Base.getindex(ts::TSFrame, i::Int, j::Int)
256257
return ts.coredata[i,j+1]
257258
end
258259

259-
function Base.getindex(ts::TSFrame, i::Int, j::Union{Symbol, String})
260+
function Base.getindex(ts::TSFrame, i::Int, j::Union{Symbol, AbstractString})
260261
return ts.coredata[i, j]
261262
end
262263

@@ -265,29 +266,40 @@ function Base.getindex(ts::TSFrame, dt::T, j::Int) where {T<:TimeType}
265266
ts.coredata[idx, j+1]
266267
end
267268

268-
function Base.getindex(ts::TSFrame, dt::T, j::Union{String, Symbol}) where {T<:TimeType}
269+
function Base.getindex(ts::TSFrame, dt::T, j::Union{AbstractString, Symbol}) where {T<:TimeType}
269270
idx = findfirst(x -> x == dt, index(ts))
270271
ts.coredata[idx, j]
271272
end
272273
###
273274

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

279-
function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{T}) where {T<:Union{String, Symbol}}
280-
TSFrame(ts.coredata[[i], Cols(:Index, j)])
280+
281+
function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
282+
if length(unique(map(x -> typeof(x), j))) == 1
283+
TSFrame(ts.coredata[[i], Cols(:Index, j)])
284+
else
285+
throw(ArgumentError("The column vector cannot contain both String and Symbol types."))
286+
end
281287
end
282288

289+
283290
function Base.getindex(ts::TSFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType}
284291
idx = findfirst(x -> x == dt, index(ts))
285292
ts[idx, j]
286293
end
287294

288-
function Base.getindex(ts::TSFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}}
295+
function Base.getindex(ts::TSFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{AbstractString, Symbol}}
289296
idx = findfirst(x -> x == dt, index(ts))
290-
ts[idx, j]
297+
if length(unique(map(x -> typeof(x), j))) == 1
298+
ts[idx, j]
299+
else
300+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
301+
end
302+
291303
end
292304
###
293305

@@ -302,7 +314,8 @@ function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Int)
302314
ts.coredata[i, j+1] # increment: account for Index
303315
end
304316

305-
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Union{String, Symbol})
317+
318+
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Union{AbstractString, Symbol})
306319
ts.coredata[i, j]
307320
end
308321

@@ -314,12 +327,18 @@ function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Int) where {T<:Tim
314327
ts[idx, j]
315328
end
316329

317-
function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Union{String, Symbol}) where {T<:TimeType}
330+
331+
function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Union{AbstractString, Symbol}) where {T<:TimeType}
318332
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
319333
if length(idx) == 0
320334
return nothing
335+
else
336+
if length(unique(map(x -> typeof(x), j))) == 1
337+
ts[idx, j]
338+
else
339+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
340+
end
321341
end
322-
ts[idx, j]
323342
end
324343
###
325344

@@ -328,10 +347,16 @@ function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{In
328347
TSFrame(ts.coredata[i, Cols(:Index, j.+1)]) # increment: account for Index
329348
end
330349

331-
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{String, Symbol}}
332-
TSFrame(ts.coredata[i, Cols(:Index, j)])
350+
351+
function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
352+
if length(unique(map(x -> typeof(x), j))) == 1
353+
TSFrame(ts.coredata[i, Cols(:Index, j)])
354+
else
355+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
356+
end
333357
end
334358

359+
335360
function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::AbstractVector{Int}) where {T<:TimeType}
336361
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
337362
if length(idx) == 0
@@ -340,9 +365,15 @@ function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::AbstractVector{Int
340365
ts[idx, j]
341366
end
342367

343-
function Base.getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}}
368+
369+
function Base.getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{AbstractString, Symbol}}
344370
idx = map(d -> findfirst(x -> x == d, index(ts)), dt)
345-
ts[idx, j]
371+
if length(unique(map(x -> typeof(x), j))) == 1
372+
ts[idx, j]
373+
else
374+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
375+
end
376+
346377
end
347378
###
348379

@@ -362,7 +393,8 @@ function Base.getindex(ts::TSFrame, i::UnitRange, j::Int)
362393
ts[collect(i), j]
363394
end
364395

365-
function Base.getindex(ts::TSFrame, i::UnitRange, j::Union{String, Symbol})
396+
397+
function Base.getindex(ts::TSFrame, i::UnitRange, j::Union{AbstractString, Symbol})
366398
ts[collect(i), j]
367399
end
368400
###
@@ -372,8 +404,14 @@ function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{Int})
372404
ts[collect(i), j]
373405
end
374406

375-
function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{T}) where {T<:Union{String, Symbol}}
376-
ts[collect(i), j]
407+
408+
function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
409+
if length(unique(map(x -> typeof(x), j))) == 1
410+
ts[collect(i), j]
411+
else
412+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
413+
end
414+
377415
end
378416
###
379417

@@ -476,7 +514,8 @@ function Base.getindex(ts::TSFrame, ::Colon, j::Int)
476514
ts[1:TSFrames.nrow(ts), j]
477515
end
478516

479-
function Base.getindex(ts::TSFrame, ::Colon, j::Union{String, Symbol})
517+
518+
function Base.getindex(ts::TSFrame, ::Colon, j::Union{AbstractString, Symbol})
480519
ts[1:TSFrames.nrow(ts), j]
481520
end
482521
###
@@ -486,8 +525,13 @@ function Base.getindex(ts::TSFrame, ::Colon, j::AbstractVector{Int})
486525
ts[1:TSFrames.nrow(ts), j]
487526
end
488527

489-
function Base.getindex(ts::TSFrame, ::Colon, j::AbstractVector{T}) where {T<:Union{String, Symbol}}
490-
ts[1:TSFrames.nrow(ts), j]
528+
function Base.getindex(ts::TSFrame, ::Colon, j::AbstractVector{T}) where {T<:Union{AbstractString, Symbol}}
529+
if length(unique(map(x -> typeof(x), j))) == 1
530+
ts[1:TSFrames.nrow(ts), j]
531+
else
532+
throw(ArgumentError("The column vector cannot contain both AbstractString and Symbol types."))
533+
end
534+
491535
end
492536
###
493537

0 commit comments

Comments
 (0)