ardaar
ardaar

Reputation: 1274

Get JuliaDB.loadtable() to parse all columns as String

I want JuliaDB.loadtable() to read a CSV (really a bunch of CSVs, but for simplicity let's try just one), where all columns are parsed as String.

Here's what I've tried:

using CSV
using DataFrames
using JuliaDB

df1 = DataFrame(
    [['a', 'b', 'c'], [1, 2, 3]],
    ["name", "id"]
)

CSV.write("df1.csv", df1)

# This works, but if I have 10+ columns it would get unwieldy
df1 = loadtable("df1.csv"; colparsers=Dict(:name=>String, :id=>String),)

# This doesn't work
df1 = loadtable("df1.csv"; colparsers=String,)
# MethodError: no method matching iterate(::Type{String})

Here's how it's done in R:

df1 = read.csv("df1.csv", colClasses = "character")

Upvotes: 1

Views: 130

Answers (1)

Igor Rivin
Igor Rivin

Reputation: 4864

If you know the number of columns (or just an upper bound on it), you can use types, I should think (from CSV.jl documentation):

types: a Vector or Dict of types to be used for column types; a Dict can map column index Int, or name Symbol or String to type for a column, i.e. Dict(1=>Float64) will set the first column as a Float64, Dict(:column1=>Float64) will set the column named column1 to Float64 and, Dict("column1"=>Float64) will set the column1 to Float64; if a Vector if provided, it must match the # of columns provided or detected in header

Upvotes: 0

Related Questions