Skip to content

Latest commit

 

History

History
65 lines (52 loc) · 1.98 KB

File metadata and controls

65 lines (52 loc) · 1.98 KB

Write the data

Starting with a small test file.

-- cabal: build-depends: dataframe, text, directory
-- cabal: default-extensions: OverloadedStrings, TypeApplications, TemplateHaskell
-- cabal: default-extensions: DataKinds, TypeFamilies, TypeOperators, FlexibleInstances
-- cabal: default-extensions: FlexibleContexts, ScopedTypeVariables, UndecidableInstances
import qualified DataFrame as D
import qualified DataFrame.Lazy as L
import DataFrame.Operators
import Data.Text (Text)
import System.Directory (removeFile)

writeFile "./customers.csv" $ unlines
    [ "id,name,surname,dob"
    , "1,Ada,Lovelace,1815-12-10"
    , "2,Alan,Turing,1912-06-23"
    , "3,Grace,Hopper,1906-12-09"
    ]

Driving the schema off the record

deriveSchemaValues builds the Schema value from the record; deriveSchemaFromType adds the HasSchema instance toRecords needs.

data Customer = Customer
    { customerId :: Int
    , customerName :: Text
    }
    deriving (Show, Eq)
$(D.deriveSchemaValues ''Customer)
$(D.deriveSchemaFromType ''Customer)

customerSchema

We can now pass this to the typed CSV reader.

import qualified DataFrame.Typed as DT
import qualified DataFrame.Typed.IO.CSV as TCSV

typed <- TCSV.readCsv @(D.Schema Customer) "./customers_snake.csv"
D.columnNames (DT.thaw typed)