5

So I simply want to write a DataFrame to a csv file (or xlsx). But after executing the code to populate the DataFrame and being able to see it in the console, I cannot save it. (neither to .csv or .xlsx with the CSV package or the xlsx package loaded).

MWE:

using DataFrames
using CSV
df = DataFrame([String,Float64,Float64],["a","b","c"], 1000)
CSV.write("Trial_Julia_Output.csv", df)

This fails with

UndefRefError: access to undefined reference

What am I doing wrong?

Stacktrace:

 [1] getindex(::Array{String,1}, ::Int64) at .\array.jl:809
 [2] getindex at C:\Users\chris\.julia\packages\DataFrames\oQ5c7\src\dataframe\dataframe.jl:400 [inlined]
 [3] getindex at C:\Users\chris\.julia\packages\DataFrames\oQ5c7\src\dataframerow\dataframerow.jl:212 [inlined]
 [4] getcolumn(::DataFrameRow{DataFrame,DataFrames.Index}, ::Symbol) at C:\Users\chris\.julia\packages\DataFrames\oQ5c7\src\other\tables.jl:31
 [5] getcolumn at C:\Users\chris\.julia\packages\Tables\UxLRG\src\Tables.jl:101 [inlined]
 [6] eachcolumn at C:\Users\chris\.julia\packages\Tables\UxLRG\src\utils.jl:70 [inlined]
 [7] writerow(::Array{UInt8,1}, ::Base.RefValue{Int64}, ::Int64, ::IOStream, ::Tables.Schema{(:a, :b, :c),Tuple{String,Float64,Float64}}, ::DataFrameRow{DataFrame,DataFrames.Index}, ::Int64, ::CSV.Options{UInt8,UInt8,Nothing,Tuple{},CSV.var"#60#63"}) at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:342
 [8] #66 at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:215 [inlined]
 [9] (::CSV.var"#73#74"{CSV.var"#66#67"{Bool,Bool,Tables.Schema{(:a, :b, :c),Tuple{String,Float64,Float64}},DataFrames.DataFrameRows{DataFrame,DataFrames.Index},CSV.Options{UInt8,UInt8,Nothing,Tuple{},CSV.var"#60#63"},Tuple{Symbol,Symbol,Symbol},Int64,Int64,Array{UInt8,1}}})(::IOStream) at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:279
 [10] open(::CSV.var"#73#74"{CSV.var"#66#67"{Bool,Bool,Tables.Schema{(:a, :b, :c),Tuple{String,Float64,Float64}},DataFrames.DataFrameRows{DataFrame,DataFrames.Index},CSV.Options{UInt8,UInt8,Nothing,Tuple{},CSV.var"#60#63"},Tuple{Symbol,Symbol,Symbol},Int64,Int64,Array{UInt8,1}}}, ::String, ::Vararg{String,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at .\io.jl:325
 [11] open at .\io.jl:323 [inlined]
 [12] with at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:278 [inlined]
 [13] #write#65 at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:205 [inlined]
 [14] write(::String, ::DataFrame; delim::Char, quotechar::Char, openquotechar::Nothing, closequotechar::Nothing, escapechar::Char, newline::Char, decimal::Char, dateformat::Nothing, quotestrings::Bool, missingstring::String, transform::CSV.var"#60#63", bom::Bool, append::Bool, writeheader::Nothing, partition::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:191
 [15] write(::String, ::DataFrame) at C:\Users\chris\.julia\packages\CSV\CJfFO\src\write.jl:152
 [16] top-level scope at In[36]:2
 [17] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
seulberg1
  • 955
  • 1
  • 7
  • 19

1 Answers1

3

The problem is that the data frame that you are creating has undefined elements in the first column:

julia> df = DataFrame([String, Float64, Float64], ["a","b","c"], 2)
2×3 DataFrame
│ Row │ a      │ b        │ c            │
│     │ String │ Float64  │ Float64      │
├─────┼────────┼──────────┼──────────────┤
│ 1   │ #undef │ 5.0e-324 │ 2.33319e-314 │
│ 2   │ #undef │ 0.0      │ 2.33319e-314 │

julia> df[1, 1]
ERROR: UndefRefError: access to undefined reference

If you define those elements, then you can write the data frame to a CSV file:

julia> df.a .= ["hello", "world"]
2-element Array{String,1}:
 "a"
 "b"

julia> CSV.write("df.csv", df)
"df.csv"
Cameron Bieganek
  • 7,208
  • 1
  • 23
  • 40