Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Last active Sep 25, 2015
Embed
What would you like to do?
type DataFrame{N,D}
data::D
end
immutable Field{s}
end
Field(s::Symbol) = Field{s}()
function DataFrame(;kwds...)
names = Any[]
data = Any[]
types = Any[]
for (n, d) in kwds
push!(names,n)
push!(data,d)
push!(types,typeof(d))
end
N = tuple(names...)
T = tuple(types...)
DataFrame{N,T}(tuple(data...))
end
stagedfunction getindex{N,D,s}(d::DataFrame{N,D},f::Field{s})
m = Dict(zip(N,1:length(N)))
j = m[s]
:(d.data[$j])
end
stagedfunction getindex{N,D,s}(d::DataFrame{N,D},i::Integer,f::Field{s})
m = Dict(zip(N,1:length(N)))
j = m[s]
:(d.data[$j][i])
end
stagedfunction getindex{N,D}(d::DataFrame{N,D},i::Integer)
Expr(:tuple,[:(d.data[$j][i]) for j in 1:length(D)]...)
end
d = DataFrame(aa=[1,2,3],bb=[4.0,5.0,6.0])
@code_typed d[Field(:aa)]
@code_typed d[Field(:bb)]
@code_typed d[1,Field(:aa)]
@code_typed d[1,Field(:bb)]
@code_typed d[1]
@SabrinaJaye
Copy link

SabrinaJaye commented Sep 25, 2015

Copying this into REPL results in an error:

julia> d = DataFrame(aa=[1,2,3],bb=[4.0,5.0,6.0])
ERROR: TypeError: DataFrame: in parameter, expected Type{T}, got Tuple{DataType,DataType}
 in DataFrame at none:12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment