Call a constructor by interpolation

I need to programmatically initialize an empty DataFrame using a vector of names and a vector of types:

Here is an example of doing this explicitly:

0×2 DataFrame

MWE for doing this programmatically:

nm = ["mystrings","myints"]
tps = (String,Int64)
expr = :(DataFrame($nm .= $tps))

ERROR: MethodError: Cannot `convert` an object of type Type{String} to an object of type String

Dataframe has a constructor DataFrame(column_eltypes, names), so you can do

julia> nm = ["mystrings", "myints"];

julia> tps = [String, Int64];

julia> df = DataFrame(tps, nm);

julia> names(df)
2-element Array{String,1}:

I didn’t use eval above because I think it’s a bad idea. Is there any reason why you think it would be necessary in order to do what you’re trying to do?

1 Like

Perfect thanks, I only tried quoting because I didn’t know about DataFrame(column_eltypes, names)

1 Like