代码之家  ›  专栏  ›  技术社区  ›  djangofan

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

  •  0
  • djangofan  · 技术社区  · 6 年前

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

    using Dates, CSV, DataFrames
    dfmt = dateformat"yyyy-mm-dd hh:MM:ss"
    column_types = Dict(:pickup_datetime=>DateTime, :dropoff_datetime=>DateTime)
    df = convert(DataFrame, CSV.read("$(Base.source_dir())/small_taxi.csv", 
      types=column_types, dateformat=dfmt))
    function reduce_dataframe(data_frame)
      return data_frame[[:vendor_id, :pickup_datetime, :dropoff_datetime, 
        :passenger_count, :trip_distance]]
    end
    df = reduce_dataframe(df)
    head(df)
    

    julia> include("hello.jl")
    Started ...
    elapsed CPU time: 0.09325 seconds
      0.094642 seconds (548.85 k allocations: 10.445 MiB)
    6×4 DataFrame
    │ Row │ vendor_id │ pickup_datetime     │ dropoff_datetime    │ passenger_count │
    │     │ Int64⍰    │ String⍰             │ String⍰             │ Int64⍰          │
    ├─────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┤
    │ 1   │ 1         │ 2017-01-01 01:21:25 │ 2017-01-01 01:51:56 │ 2               │
    │ 2   │ 1         │ 2017-01-01 02:17:49 │ 2017-01-01 02:17:49 │ 3               │
    │ 3   │ 1         │ 2017-01-01 02:30:02 │ 2017-01-01 02:52:56 │ 1               │
    │ 4   │ 1         │ 2017-01-01 04:17:32 │ 2017-01-01 04:17:36 │ 1               │
    │ 5   │ 1         │ 2017-01-01 04:41:54 │ 2017-01-01 05:24:22 │ 1               │
    │ 6   │ 1         │ 2017-01-01 10:41:18 │ 2017-01-01 10:56:59 │ 2               │
    

    这里有什么诀窍?如果您想亲自尝试,以下是一些示例数据: https://gist.github.com/djangofan/09c6304b55f2a73cb05d0d2afc7902b1

    2 回复  |  直到 6 年前
        1
  •  3
  •   mbeltagy    6 年前

    当面对这样的转换问题时,最好是低一点来理解正在发生的事情。

    dt_str="2017-01-01 01:21:25"
    

    它能用我们的格式字符串格式化吗?

    dfmt = dateformat"yyyy-MM-dd hh:mm:ss"
    Date(dt_str,dfmt)
    

    运行我们得到的

    ERROR: ArgumentError: Unable to parse date time. Expected directive Delim( hh:) at char 11
    

    manual . 手册指向 Dates.DateFormat 大量的例子 stdlib/Dates/test/io.jl .

    dfmt = dateformat"yyyy-mm-dd HH:MM:SS"
    Date(dt_str,dfmt)
    

    这次没有错误!我们在桌子上试试

    t_data=CSV.read("$(Base.source_dir())/small_taxi.csv", dateformat=dfmt) t_data[:vendor_id, :pickup_datetime, :dropoff_datetime, :passenger_count, :trip_distance]

    我们得到 julia> t_data[[:vendor_id, :pickup_datetime, :dropoff_datetime, :passenger_count]] 5×4 DataFrame │ Row │ vendor_id │ pickup_datetime │ dropoff_datetime │ passenger_count │ │ │ Int64⍰ │ DateTime⍰ │ DateTime⍰ │ Int64⍰ │ ├─────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┤ │ 1 │ 2 │ 2017-09-23T05:08:42 │ 2017-09-23T05:27:39 │ 6 │ │ 2 │ 1 │ 2017-07-14T19:07:38 │ 2017-07-14T19:54:17 │ 1 │ │ 3 │ 2 │ 2017-10-29T00:42:06 │ 2017-10-29T00:43:12 │ 2 │ │ 4 │ 2 │ 2017-10-02T20:38:17 │ 2017-10-02T21:13:09 │ 1 │ │ 5 │ 1 │ 2017-05-11T22:53:11 │ 2017-05-11T23:27:53 │ 2 │

        2
  •  3
  •   Uki D. Lucas    4 年前

    # import Pkg; Pkg.add("CSV")
    using CSV
    
    # import Pkg; Pkg.add("Dates")
    using Dates
    
    # import Pkg; Pkg.add("DataFrames")
    using DataFrames
    

    日期格式取决于CSV文件中的原始数据。

    注:“u”代表3个字母的英语月,例如“2020年8月3日”

    date_format="yyyy.mm.dd" # or "yyyy-mm-dd" or "u. dd, yyyy"
    

    df = CSV.read(        # returns DataFrame
            file_path,    # URL
            dateformat="$date_format"
            )
    

    输出示例:

    82 rows × 4 columns
    
    Date    ActualValue ForecastValue   PreviousValue
    Date    Float64 Float64?    Float64?
    1   2020-08-03  44.3    34.4    42.1
    
        3
  •  1
  •   Bill Antonello    6 年前

    我认为他们更改了julia1.0中的宏,因此dateformat语句形式是

    dfmt = @dateformat_str("yyyy-mm-dd HH:MM:SS")
    

    dfmt = dateformat"yyyy-mm-dd HH:MM:SS"
    

    using Dates, CSV, DataFrames
    dfmt = dateformat"yyyy-mm-dd hh:MM:ss"
    df = convert(DataFrame, CSV.read("$(Base.source_dir())/small_taxi.csv", 
        dateformat=dfmt, delim="\t", ignorerepeated=true))
    function reduce_dataframe(data_frame)
        return data_frame[[:vendor_id, :pickup_datetime, :dropoff_datetime, 
            :passenger_count, :trip_distance]]
    end
    df = reduce_dataframe(df)
    head(df)