Having trouble with plot and vcat inside a Julia script

Viewed 65

I must be doing something wrong. I have a Julia script (below) that uses both vcat and plot. When I run the script, vcat returns an empty DataFrame. Another function calls plot and no plot is generated.

When I manually type the commands in the terminal window the commands behave normally.

Any help would be appreciated.

f_l = file_list[start_row_num:end_row_num] # Build a dataframe containing the data
len = length(f_l)
tmp_stock_df = DataFrame(CSV.File(f_l[1]))
vcat(s_d_df, tmp_stock_df)
println(s_d_df)
for i = 2:len
    tmp_stock_df = DataFrame(CSV.File(f_l[i]))
    tmp_stock_df.quote_datetime = map((x) -> DateTime(x, "yyyy-mm-dd HH:MM:SS"), tmp_stock_df.quote_datetime)
    DataFrames.vcat(s_d_df, tmp_stock_df)
end
1 Answers

It's hard to say what you're doing differently when manually typing in the commands, but it seems to me that this code would ever produce the results you're looking for. Apart from the fact that s_d_df is not defined, vcat does not mutate its arguments, and therefore you're never actually adding to your DataFrame:

julia> using DataFrames

julia> df1 = DataFrame(a = rand(2), b = rand(2)); df2 = DataFrame(a = rand(2), b = rand(2));

julia> vcat(df1, df2)
4×2 DataFrame
 Row │ a         b        
     │ Float64   Float64  
─────┼────────────────────
   1 │ 0.918298  0.343344 
   2 │ 0.538763  0.188229 
   3 │ 0.347177  0.385166 
   4 │ 0.18795   0.98408  

julia> df1
2×2 DataFrame
 Row │ a         b        
     │ Float64   Float64  
─────┼────────────────────
   1 │ 0.918298  0.343344
   2 │ 0.538763  0.188229

You probably want s_d_df = vcat(s_d_df, tmp_stock_df) to assign the result of the concatenation.

On a related note, it looks like you just have a list of files f_l with different csv files stored on your system which you want to read into a single DataFrame, in which case you can just replace the whole loop with:

s_d_df = vcat(CSV.read.(f_l, DataFrame)...)

(potentially also use the dateformat = "yyyy-mm-dd HH:MM:SS" kwarg in CSV.read to directly parse the dates when reading in the file).

Related