Thanks. Here is a specific minimal example with various approaches I have tried.
My objective is to simply initialize a database directly using types, and then
append to the database. The fly in the ointment is that my type is Array{Any,1}, equivalent I believe to Vector{Any}
I have commented out various approaches. The demonstrations here in consider creating a dataframe with two integer columns, in which case my type is Tuple(Int64, 64). The actual problem I am interested is when the type is Tuple(V, V) where V=Vector{Any}.
using DataFrames
#---------------------------------------------------------
function tst1()
# I added Pandas to the packages, and removed it, and yet, the functsions are still available.
# Since both Pandas and DataFrames have a DataFrame method, I am forced to prepend DataFrames to
# some or most of the DataFrames methods.
Jleak = [0.1, 0.3]
JIPR = [0.2, 0.4]
println(typeof(Jleak)) # Array{Float64,1}
V = Vector{Any}
types = ([V for i โ 1:2])
nb_el = 2
tuple1 = Tuple([[0.,0.] for i โ 1:nb_el])
tuple2 = Tuple([V for i โ 1:nb_el])
named_tuple1 = NamedTuple{(:a,:b),Tuple{Int64,Int64}}
named_tuple2 = NamedTuple{(:a,:b),Tuple{V,V}}
DF = DataFrames
# APPROACHES I HAVE TRIED
# The next five lines all give errors.
#append!(DF.DataFrame(), named_tuple1)
#push!(DF.DataFrame(), named_tuple1) # does not work
#append!(DF.DataFrame(), named_tuple2)
#df = DF.DataFrame(named_tuple1) # does not work
#df = DF.DataFrame(named_tuple2) # does not work
# However, I can initiailize the DataFrame with values
named_tuple3 = NamedTuple{(:a,:b),Tuple{1., 2.}}
named_tuple4 = NamedTuple{(:a, :b)}([1., 2.])
append!(DF.DataFrame(), named_tuple3) # does not work
append!(DF.DataFrame(), named_tuple4)
#named_tuple4 = NamedTuple{(:a,:b),Tuple{V,V}}
end
tst1()
Here is what I did get to work:
function tst2()
V = Vector{Any}
cols = (:minf, :Jleak, :JIPR, :JSERCA, :J_ฮฒ, :J_ฮด, :J5P, :J3K, :Q2, :OmegaH, :hinf)
vals = ([V for i โ 1:7])
tuple1 = Tuple([V for i โ 1:11])
tuple2 = Tuple([[0.,0.] for i โ 1:11])
println(tuple)
df = DF.DataFrame(tuple2)
append!(df, tuple2)
println(df)
println(df)
#named_tuple = NamedTuple{cols, Tuple{V,V,V,V,V,V,V,V,V,V,V}}
#push!(DF.DataFrame(), named_tuple)
end
tst()
Notice that I initialized the database with a Tuple and not a NamedTuple. I was told I could initialied a DataFrame directly with a NamedTuple. The DataFrame produced is not what I intended. The output of the second function tst2() gives:
([0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0])
4ร11 DataFrames.DataFrame
โ Row โ x1 โ x2 โ x3 โ x4 โ x5 โ x6 โ x7 โ x8 โ x9 โ x10 โ x11 โ
โ โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ Float64 โ
โโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ 1 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ
โ 2 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ
โ 3 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ
โ 4 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ 0.0 โ
This is incorrect. I was expecting only two rows, where each column was a vector of two elements, as defined by tuple2.
I hope this is clearer.