Working with multiple Dicts in a list - ERROR: MethodError: Cannot `convert` an object of type Array{Int64,1} to an object of type DataType

Hi,

I would like to read a CSV-file and then convert it to a list of dicts, where the keys can contain arrays.

But with the following code I get the error message
ERROR: MethodError: Cannot convert an object of type Array{Int64,1} to an object of type DataType

Can somebody explain me what I´m doing wrong?

using DataFrames
using CSV

df_train_plan_raw =CSV.read(<file>)
dict_train_plan = [Dict(name => Any for name in names(df_train_plan_raw) if name != :no_models)
    for i in 1:Int32(sum(df_train_plan_raw.no_models))]

dict_train_plan = [Dict(name => Any for name in names(df_train_plan_raw) if name != :no_models)
    for i in 1:Int32(sum(df_train_plan_raw.no_models))]

dict_train_plan[1][:layer_neurons] = [parse(Int64, elem) for elem in split(df_train_plan_raw.layer_neurons[1], ">")]

Edit:

dict_train_plan = [Dict(name => [] for name in names(df_train_plan_raw) if name != :no_models)
    for i in 1:Int32(sum(df_train_plan_raw.no_models))]

If I change this line, it will work for arrays, but not for scalars. Is there a data type for initializing, which work for any data type?

Best regards

This is a good case for trying to come up with an MWE - effectively your issue can be reduced down to

d = Dict("key1" => Any)
d["key1"] = 5

When you create dict_train_plan your call to the constructor is Dict(name => Any) which creates a dictionary that expects its values to be of type DataType.

I’m not exactly sure what you’re trying to achieve here and what do you need your final dictionaries to look like, can’t really recommend a solution.

I did type out a little bit of example data for others to reproduce the error though:

df_train_plan_raw = DataFrame(no_models = [10, 10], layer_neurons = ["30>30","30>30"], layer_types = ["dense>dense",
    "dense>dense"], activation_fcn = ["sigmoid>sigmoid","sigmoid>sigmoid"],
    train_method = ["adam","adam"], epochs = [2000, 2000], patience = [100, 100])

My goal is to get a dictionary, where I can iterate over to train several different models with keras. Therefore I would like to convert the dataframe to a dictionary, where the parameters are similiar to the arguments of the function call of keras.

But why don’t you just iterate over the DataFrame?

Because the parameters can be arrays and I don´t think, that it is possible to store arrays i. e.

df_train_plan_raw.layer_neurons[1]= [15, 15, 15]

I could convert it just right before the function call, but in my opinion from a code encapsulation view it is nicer to do it once for all before.

df1 = DataFrame(layer_neurons = [[15,15,15],[30,30,30]])
df1.layer_neurons[1] # returns [15, 15, 15]

?

1 Like

Or more directly using my example data from above:

df_train_plan_raw[!, :layer_neurons_int] = [[parse(Int, i) for i in j] for j in split.(df_train_plan_raw.layer_neurons, ">")]
df_train_plan_raw.layer_neurons_int[1] # returns [30, 30]

Thank you very much :slight_smile: I didn´t know that this possible

1 Like