如何使用 Julia 从 .CSV 文件数据读取 DateTime 数据类型

dja*_*fan 2 julia

如何使用 Julia(julia 版本 1.0.1)从 .CSV 文件数据读取 DateTime?如果您注意到这里,当它读入我的数据时,它被标记为“字符串”值,但我希望对 head() 的调用会将 DateTime 值显示为数据类型。

\n\n

我正在读这样的内容:

\n\n
using Dates, CSV, DataFrames\ndfmt = dateformat"yyyy-mm-dd hh:MM:ss"\ncolumn_types = Dict(:pickup_datetime=>DateTime, :dropoff_datetime=>DateTime)\ndf = convert(DataFrame, CSV.read("$(Base.source_dir())/small_taxi.csv", \n  types=column_types, dateformat=dfmt))\nfunction reduce_dataframe(data_frame)\n  return data_frame[[:vendor_id, :pickup_datetime, :dropoff_datetime, \n    :passenger_count, :trip_distance]]\nend\ndf = reduce_dataframe(df)\nhead(df)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的程序输出(来自出租车数据):

\n\n
julia> include("hello.jl")\nStarted ...\nelapsed CPU time: 0.09325 seconds\n  0.094642 seconds (548.85 k allocations: 10.445 MiB)\n6\xc3\x974 DataFrame\n\xe2\x94\x82 Row \xe2\x94\x82 vendor_id \xe2\x94\x82 pickup_datetime     \xe2\x94\x82 dropoff_datetime    \xe2\x94\x82 passenger_count \xe2\x94\x82\n\xe2\x94\x82     \xe2\x94\x82 Int64\xe2\x8d\xb0    \xe2\x94\x82 String\xe2\x8d\xb0             \xe2\x94\x82 String\xe2\x8d\xb0             \xe2\x94\x82 Int64\xe2\x8d\xb0          \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 1   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 01:21:25 \xe2\x94\x82 2017-01-01 01:51:56 \xe2\x94\x82 2               \xe2\x94\x82\n\xe2\x94\x82 2   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 02:17:49 \xe2\x94\x82 2017-01-01 02:17:49 \xe2\x94\x82 3               \xe2\x94\x82\n\xe2\x94\x82 3   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 02:30:02 \xe2\x94\x82 2017-01-01 02:52:56 \xe2\x94\x82 1               \xe2\x94\x82\n\xe2\x94\x82 4   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 04:17:32 \xe2\x94\x82 2017-01-01 04:17:36 \xe2\x94\x82 1               \xe2\x94\x82\n\xe2\x94\x82 5   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 04:41:54 \xe2\x94\x82 2017-01-01 05:24:22 \xe2\x94\x82 1               \xe2\x94\x82\n\xe2\x94\x82 6   \xe2\x94\x82 1         \xe2\x94\x82 2017-01-01 10:41:18 \xe2\x94\x82 2017-01-01 10:56:59 \xe2\x94\x82 2               \xe2\x94\x82\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里有什么技巧呢?如果您想自己尝试,这里有一些示例数据: https://gist.github.com/djangofan/09c6304b55f2a73cb05d0d2afc7902b1

\n

小智 5

当遇到此类转换问题时,最好稍微深入一点以了解发生了什么。

\n

因此,我们首先从表中查找日期时间字符串

\n
dt_str="2017-01-01 01:21:25"\n
Run Code Online (Sandbox Code Playgroud)\n

可以用我们的格式字符串格式化吗?

\n
dfmt = dateformat"yyyy-MM-dd hh:mm:ss"\nDate(dt_str,dfmt)\n
Run Code Online (Sandbox Code Playgroud)\n

运行我们得到

\n
ERROR: ArgumentError: Unable to parse date time. Expected directive Delim( hh:) at char 11\n
Run Code Online (Sandbox Code Playgroud)\n

这里有些不对劲。让我们查阅手册。该手册指向Dates.DateFormat和stdlib/Dates/test/io.jl 中的大量示例

\n

我们注意到我们已经在数月、数小时、数秒内使用了错误的字母。我们现在测试

\n
dfmt = dateformat"yyyy-mm-dd HH:MM:SS"\nDate(dt_str,dfmt)\n
Run Code Online (Sandbox Code Playgroud)\n

这次没有错误!我们在我们的桌子上尝试一下

\n
t_data=CSV.read("$(Base.source_dir())/small_taxi.csv", dateformat=dfmt)\nt_data[:vendor_id, :pickup_datetime, :dropoff_datetime, \n        :passenger_count, :trip_distance]\n
Run Code Online (Sandbox Code Playgroud)\n

我们得到

\n
julia> t_data[[:vendor_id, :pickup_datetime, :dropoff_datetime, \n               :passenger_count]]\n5\xc3\x974 DataFrame\n\xe2\x94\x82 Row \xe2\x94\x82 vendor_id \xe2\x94\x82 pickup_datetime     \xe2\x94\x82 dropoff_datetime    \xe2\x94\x82 passenger_count \xe2\x94\x82\n\xe2\x94\x82     \xe2\x94\x82 Int64\xe2\x8d\xb0    \xe2\x94\x82 DateTime\xe2\x8d\xb0           \xe2\x94\x82 DateTime\xe2\x8d\xb0           \xe2\x94\x82 Int64\xe2\x8d\xb0          \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 1   \xe2\x94\x82 2         \xe2\x94\x82 2017-09-23T05:08:42 \xe2\x94\x82 2017-09-23T05:27:39 \xe2\x94\x82 6               \xe2\x94\x82\n\xe2\x94\x82 2   \xe2\x94\x82 1         \xe2\x94\x82 2017-07-14T19:07:38 \xe2\x94\x82 2017-07-14T19:54:17 \xe2\x94\x82 1               \xe2\x94\x82\n\xe2\x94\x82 3   \xe2\x94\x82 2         \xe2\x94\x82 2017-10-29T00:42:06 \xe2\x94\x82 2017-10-29T00:43:12 \xe2\x94\x82 2               \xe2\x94\x82\n\xe2\x94\x82 4   \xe2\x94\x82 2         \xe2\x94\x82 2017-10-02T20:38:17 \xe2\x94\x82 2017-10-02T21:13:09 \xe2\x94\x82 1               \xe2\x94\x82\n\xe2\x94\x82 5   \xe2\x94\x82 1         \xe2\x94\x82 2017-05-11T22:53:11 \xe2\x94\x82 2017-05-11T23:27:53 \xe2\x94\x82 2               \xe2\x94\x82\n
Run Code Online (Sandbox Code Playgroud)\n