Julia-加速字符串到日期或日期时间的转换

如何在 Julia 中加快字符串到日期时间的转换?需要很长时间,并且在进程中分配了大量内存?

回答

当您转换字符串的列或向量时,请在单独的变量中定义字符串格式,然后将此变量作为函数 Dates.DateTime 中的第二个变量传递。假设您的字符串在 DataFrame 列中df.Date,那么

代替:

df.DateTime = Dates.DateTime.(df.Date , "yyy-mm-dd HH:MM:SS")

和:

myFormat = Dates.DateFormat("yyy-mm-dd HH:MM:SS")
df.DateTime = Dates.DateTime.(df.Date , myFormat)

这显着加快了转换速度(在我的情况下,对于 30k 元素向量是 20 倍)。

感谢 discourse.julialang 上的用户 BioTurboNick 解决了这个问题。原因在文档中。基本上在前一种情况下,julia 为每个单独的转换创建一个 DateFormat 对象,从而大大增加了内存分配。

DateTime(dt::AbstractString, format::AbstractString; locale="english") -> DateTime

  Construct a DateTime by parsing the dt date time string following the pattern given in the format string (see
  DateFormat for syntax).

  This method creates a DateFormat object each time it is called. If you are parsing many date time strings of the
  same format, consider creating a DateFormat object once and using that as the second argument instead.```


以上是Julia-加速字符串到日期或日期时间的转换的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>