setindex!朱莉娅的错误?在日期时间使用“ For循环”时

时间:2020-05-22 15:41:57

标签: machine-learning julia artificial-intelligence ijulia-notebook

我从 Coursera 学到了 Julia ,当我到达第二周的演讲课时,笔记本中出现了一些代码错误。 它给了我 setindex!没有为WeakRefStrings.StringArray {String,1} 错误定义。

Csv File import here

using CSV, Dates
wikiEVDraw = CSV.read("wikipediaEVDraw1.csv")

Dates.DateTime.(wikiEVDraw[1,1],Ref("d-u-y"))

Date Code here

col1=wikiEVDraw[:,1] 

setindex! error here

for i = 1:length(col1)
    col1[i]=Dates.DateTime.(col1[i],Ref("d-u-y"))
end

1 个答案:

答案 0 :(得分:1)

这是因为您第二次忘记了格式字符串中的破折号(-)。

请注意,您的代码中还有另一个错误:col1的类型为WeakRefStrings.StringArray,它不是(AFAIU)实际上不是要处理的数据类型。而且在任何情况下,它都是用于存储字符串的,因此您不能使用DateTime对象替换其元素。

相反,您可以构建整个列(作为副本),并将每个元素转换为DateTime对象。广播语法可能是最惯用的方法:

# Sample data
julia> col = ["01-Jan-2020", "02-Jan-2020", "03-Jan-2020"]
3-element Array{String,1}:
 "01-Jan-2020"
 "02-Jan-2020"
 "03-Jan-2020"

julia> using Dates

       # Note the '.' before the open parenthesis
julia> col = Dates.DateTime.(col, Ref("d-u-y"))
3-element Array{DateTime,1}:
 2020-01-01T00:00:00
 2020-01-02T00:00:00
 2020-01-03T00:00:00