How to specify `CSV.read` column types?

How should column types be specified for CSV.read?

This is just some toy data, but I can’t seem to specify the types for it.

using CSV
iob = IOBuffer("9000,4,473,6473,[1, 1, 0, 1]
9001,4,326,6240,[0, 0, 1, 1]
9002,4,196,6273,[1, 0, 0, 0]")
df = CSV.read(iob, datarow=1)

	Column1	Column2	Column3	Column4	Column5	Column6	Column7	Column8
1      9000       4     473    6473      [1       1       0       1]
2      9001       4     326    6240      [0       0       1       1]
3      9002       4     196    6273      [1       0       0       0]

iob.ptr = 1
df2 = CSV.read(iob, datarow=1, types=fill(Any,8)')

ArgumentError: 8 number of columns detected; `types` argument has 8 entries

Stacktrace:
 [1] #Source#24(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::CSV.Options{Void}, ::Int64, ::Int64, ::RowVector{Any,ConjArray{DataType,1,Array{DataType,1}}}, ::Symbol, ::Void, ::Bool, ::Void, ::Symbol, ::Int64, ::Int64, ::Int64, ::Bool, ::Type{T} where T) at /home/rick/.julia/v0.6/CSV/src/Source.jl:188
 [2] (::Core.#kw#Type)(::Array{Any,1}, ::Type{CSV.Source}) at ./<missing>:0
 [3] #Source#23(::UInt8, ::UInt8, ::UInt8, ::String, ::Void, ::Int64, ::Int64, ::RowVector{Any,ConjArray{DataType,1,Array{DataType,1}}}, ::Symbol, ::Void, ::Void, ::UInt8, ::String, ::String, ::Bool, ::Void, ::Symbol, ::Int64, ::Int64, ::Int64, ::Bool, ::Type{T} where T, ::Base.AbstractIOBuffer{Array{UInt8,1}}) at /home/rick/.julia/v0.6/CSV/src/Source.jl:31
 [4] (::Core.#kw#Type)(::Array{Any,1}, ::Type{CSV.Source}, ::Base.AbstractIOBuffer{Array{UInt8,1}}) at ./<missing>:0
 [5] #read#43(::Bool, ::Dict{Int64,Function}, ::Bool, ::Array{Any,1}, ::Function, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Type{T} where T) at /home/rick/.julia/v0.6/CSV/src/Source.jl:347
 [6] (::CSV.#kw##read)(::Array{Any,1}, ::CSV.#read, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Type{T} where T) at ./<missing>:0 (repeats 2 times)
 [7] include_string(::String, ::String) at ./loading.jl:522

The problem is in the Any which is an abstract type. Julia can’t parse something into Any. If you switch that to, say, Int, it’ll work. Also, remove the transpose ', it’s not needed.

1 Like

Thanks!

Is there an easy way to interpret the last 4 columns in each row of the example data as a single Vector{Int64}?

Nothing like that for now.

In the long run I think CSV needs to be a lot more hackable.

2 Likes