I seem to be missing something here. I thought I understood how push works but obviously not. Can someone help please.
I have a dataframe defined
describe(df_dash_table)
9×7 DataFrame
Row │ variable mean min median max nmissing eltype
│ Symbol Union… Nothing Nothing Nothing Int64 DataType
─────┼─────────────────────────────────────────────────────────────────
1 │ sym 0 String
2 │ price NaN 0 Float64
3 │ sdmove NaN 0 Float64
4 │ hv20 NaN 0 Float64
5 │ hv10 NaN 0 Float64
6 │ hv5 NaN 0 Float64
7 │ iv NaN 0 Float64
8 │ iv%ile NaN 0 Float64
9 │ prc%ile NaN 0 Float64
In the code below a ZMQ message comes in and is parsed into 3 components.
message = "IND~SPX~LAST~4382.46"
source,sym_in,field_in ,value_in = split( message , "~")
and I want to update the df_dash_table using the components like this.
df[findfirst(==(sym_in),df.id),findfirst(==(field_in),names(df))] = value_in
this won’t work because sym_in ( in this case “SPX”) doesn’t exist and so I want to add a seed row so I can update it. So the seed row would be
"SPX" , 0.0, 0.0, 0.0 , 0.0, 0.0 , 0.0 , 0.0 , 0.0
using
push!(df_dash_table, (sym_in, 0.0, 0.0, 0.0 , 0.0, 0.0 , 0.0 , 0.0 , 0.0 ) )
the update the new row
df[findfirst(==(sym_in),df_dash_table.sym),findfirst(==(field_in),names(df_dash_table))] = value_in
but this yields the error
Error: Error adding value to column :price.
└ @ DataFrames ~/.julia/packages/DataFrames/zqFGs/src/dataframe/dataframe.jl:1719
ERROR: AssertionError: length(col) == targetrows
Stacktrace:
[1] push!(df::DataFrame, row::Tuple{SubString{String}, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64}; promote::Bool)
@ DataFrames ~/.julia/packages/DataFrames/zqFGs/src/dataframe/dataframe.jl:1712
[2] push!(df::DataFrame, row::Tuple{SubString{String}, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64})
@ DataFrames ~/.julia/packages/DataFrames/zqFGs/src/dataframe/dataframe.jl:1680
[3] top-level scope
@ REPL[12]:7
I don’t see why? When I read the push docs it seems to me that I should be able to be able to add the seed row. What am I doing wrong and is there a better way to handle this?
using DataFrames
dash_columns = ["sym","price","sdmove","hv20","hv10","hv5","iv","iv%ile","prc%ile"]
df_dash_table = DataFrame(fill( Float64[],length( dash_columns[2:end] )), dash_columns[2:end]; copycols=false)
insertcols!(df_dash_table,1, :sym => "")
message = "IND~SPX~PRICE~4382.46"
source,sym_in,field_in ,value_in = split( message , "~")
push!(df, (sym_in, 0.0, 0.0, 0.0 , 0.0, 0.0 , 0.0 , 0.0 , 0.0 )
df_dash_table[findfirst(==(sym_in),df_dash_table.sym),findfirst(==(field_in),names(df))] = value_in
the usecase is that the next message to come in might be
message = "IND~SPX~HV20~17.76"
so df_dash_table would go from
"sym","price","sdmove","hv20","hv10","hv5","iv","iv%ile","prc%ile"
"SPX", 4382.46, 0.0, 0.0 , 0.0, 0.0 , 0.0 , 0.0 , 0.0
TO ( updating “SPX” and “hv20” with 17.76)
"sym","price","sdmove","hv20","hv10","hv5","iv","iv%ile","prc%ile"
"SPX", 4382.46, 0.0, 17.76 , 0.0, 0.0 , 0.0 , 0.0 , 0.0