dataframe-csv-2.3.0.0: CSV reader and writer for the dataframe ecosystem.
Safe HaskellNone
LanguageHaskell2010

DataFrame.Typed.IO.CSV

Description

Typed CSV reading.

The schema is the whole specification of the read: it names the columns to fetch and gives their types, so these readers touch only the columns cols declares and never infer a type the schema already knows.

type Customer = '[ '("customer_id", Int), '("customer_name", Text)]

customers <- readCsv @Customer "customers.csv"  -- reads 2 columns, whatever
                                                 -- else the file holds
Synopsis

Documentation

readCsv :: forall (cols :: [(Symbol, Type)]). (KnownSchema cols, RuntimeSchema cols) => FilePath -> IO (TypedDataFrame cols) Source #

Read a CSV file into a typed DataFrame, throwing on schema mismatch. Reads only the columns cols names, typed as cols says.

Example

Expand
ghci> type Customer = '[ '("id", Int), '("name", Text)]
ghci> customers <- readCsv @Customer "customers.csv"

readCsvWithError :: forall (cols :: [(Symbol, Type)]). (KnownSchema cols, RuntimeSchema cols) => FilePath -> IO (Either Text (TypedDataFrame cols)) Source #

Read a CSV file, returning a descriptive error on schema mismatch or a missing column instead of throwing.

Example

Expand
ghci> readCsvWithError @Customer "customers.csv"
Right (TDF ...)

readTsv :: forall (cols :: [(Symbol, Type)]). (KnownSchema cols, RuntimeSchema cols) => FilePath -> IO (TypedDataFrame cols) Source #

Read a tab-separated file into a typed DataFrame, throwing on mismatch.

Example

Expand
ghci> customers <- readTsv @Customer "customers.tsv"

readCsvWithOpts :: forall (cols :: [(Symbol, Type)]). (KnownSchema cols, RuntimeSchema cols) => ReadOptions -> FilePath -> IO (TypedDataFrame cols) Source #

Read a CSV file with custom options, throwing on schema mismatch. The schema still supplies the column selection and types; an explicit readColumns takes precedence, and is then checked by the freeze.

Example

Expand
ghci> customers <- readCsvWithOpts @Customer defaultReadOptions{safeRead = MaybeRead} "customers.csv"

writeCsv :: forall (cols :: [(Symbol, Type)]). FilePath -> TypedDataFrame cols -> IO () Source #

Write a typed DataFrame to a CSV file.

Example

Expand
ghci> writeCsv "customers.csv" customers

writeTsv :: forall (cols :: [(Symbol, Type)]). FilePath -> TypedDataFrame cols -> IO () Source #

Write a typed DataFrame to a tab-separated file.

Example

Expand
ghci> writeTsv "customers.tsv" customers