使用CSV.jl写入csv文件时会抛出StackOverflowError消息

bvjveswy  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(132)

CSV.write()返回:ERROR: LoadError: StackOverflowError:进一步声明--- the last 2 lines are repeated 39990 more times ---。这是毫无意义的,因为要写入的 Dataframe 被确认为129x6维,没有空字段。我担心该函数计算的行数超出了 Dataframe 的维度,不应该这样做。如何使该函数工作?

using CSV,DataFrames
file=DataFrame(
        a=1:50,
        b=1:50,
        c=Vector{Any}(missing,50),
        d=Vector{Any}(missing,50))
CSV.write("a.csv",file)
file=CSV.read("a.csv",DataFrame)
c,d=[],[]
for i in 1:nrow(file)
    push!(c,file.a[i]*file.b[i])
    push!(d,file.a[i]*file.b[i]/file.a[i])
end
file[!,:c]=c
file[!,:d]=d
# I found no straight forward way to fill empty columns based on the values of filled columns in a for loop that wouldn't generate error messages
CSV.write("a.csv",DataFrame) # Error in question here
wydwbb8l

wydwbb8l1#

StackOverflowError确实是由行引起的

CSV.write("a.csv",DataFrame)

它试图将数据类型本身写入文件,而不是实际的dataframe变量(导致此特定错误的原因是here中描述的实现细节)。
# I found no straight forward way to fill empty columns based on the values of filled columns in a for loop that wouldn't generate error messages
您不需要为此使用for循环,只需

file.c = file.a .* file.b
file.d = file.a .* file.b ./ file.a

(虽然我不明白file.d计算的意义--也许这只是您为了说明而选择的一个虚拟计算示例)。

相关问题