Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Last active September 25, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbyrne/110718f98f13dfd5d4f5 to your computer and use it in GitHub Desktop.
Save simonbyrne/110718f98f13dfd5d4f5 to your computer and use it in GitHub Desktop.
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]
@TransGirlCodes
Copy link

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