Julia 将 DataFrame 写入 csv 失败 UndefRefError: access to undefined reference

Posted

技术标签:

【中文标题】Julia 将 DataFrame 写入 csv 失败 UndefRefError: access to undefined reference【英文标题】:Julia writing DataFrame to csv fails UndefRefError: access to undefined reference 【发布时间】:2021-06-22 20:34:47 【问题描述】:

所以我只想将 DataFrame 写入 csv 文件(或 xlsx)。但是在执行代码以填充 DataFrame 并能够在控制台中看到它之后,我无法保存它。 (加载了 CSV 包或 xlsx 包的 .csv 或 .xlsx 都不适用)。

MWE:

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

这失败了

UndefRefError: access to undefined reference

我做错了什么?

堆栈跟踪:

 [1] getindex(::ArrayString,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(::DataFrameRowDataFrame,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(::ArrayUInt8,1, ::Base.RefValueInt64, ::Int64, ::iostream, ::Tables.Schema(:a, :b, :c),TupleString,Float64,Float64, ::DataFrameRowDataFrame,DataFrames.Index, ::Int64, ::CSV.OptionsUInt8,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),TupleString,Float64,Float64,DataFrames.DataFrameRowsDataFrame,DataFrames.Index,CSV.OptionsUInt8,UInt8,Nothing,Tuple,CSV.var"#60#63",TupleSymbol,Symbol,Symbol,Int64,Int64,ArrayUInt8,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),TupleString,Float64,Float64,DataFrames.DataFrameRowsDataFrame,DataFrames.Index,CSV.OptionsUInt8,UInt8,Nothing,Tuple,CSV.var"#60#63",TupleSymbol,Symbol,Symbol,Int64,Int64,ArrayUInt8,1, ::String, ::VarargString,N where N; kwargs::Base.Iterators.PairsUnion,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.PairsUnion,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

【问题讨论】:

【参考方案1】:

问题是您正在创建的数据框在第一列中有未定义的元素:

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

如果您定义了这些元素,那么您可以将数据框写入 CSV 文件:

julia> df.a .= ["hello", "world"]
2-element ArrayString,1:
 "a"
 "b"

julia> CSV.write("df.csv", df)
"df.csv"

【讨论】:

啊 - 我不知道未定义的单元格会破坏整个数据框,谢谢:)

以上是关于Julia 将 DataFrame 写入 csv 失败 UndefRefError: access to undefined reference的主要内容,如果未能解决你的问题,请参考以下文章

将 Python DataFrame 作为 CSV 写入 Azure Blob

DataFrame iterrows() 和 .to_csv:逐行写入

Julia:将 DataFrame 传递给函数会创建指向 DataFrame 的指针?

python写入csv方法总结

Pyspark - 将数据帧写入 2 个不同的 csv 文件

python写入csv文件的几种方法总结