dataframe-core-2.4.0.0: Core data structures for the dataframe library.
Safe HaskellNone
LanguageHaskell2010

DataFrame.Core

Description

The curated public surface of dataframe-core: the interchange types (DataFrame, Column, Row, Expr), element constraints, and the rendering/serialization verbs. Internal plumbing stays in dataframe-core:internal.

Synopsis

The DataFrame

data DataFrame Source #

Instances

Instances details
Show DataFrame Source # 
Instance details

Defined in DataFrame.Internal.DataFrame

ToDataFrame DataFrame Source # 
Instance details

Defined in DataFrame.Typed.Freeze

Eq DataFrame Source # 
Instance details

Defined in DataFrame.Internal.DataFrame

data GroupedDataFrame Source #

A record that contains information about how and what rows are grouped in the dataframe. This can only be used with aggregate.

empty :: DataFrame Source #

O(1) Creates an empty dataframe

fromNamedColumns :: [(Text, Column)] -> DataFrame Source #

Build a DataFrame from a list of (name, column) pairs using insertColumn.

insertColumn :: Text -> Column -> DataFrame -> DataFrame Source #

Insert a column into a DataFrame. If a column with the same name already exists it is replaced in-place; otherwise the column is appended at the end. Other columns are expanded (padded with nulls) to match the new row count.

columnNames :: DataFrame -> [Text] Source #

O(k) Get column names of the DataFrame in order of insertion.

null :: DataFrame -> Bool Source #

Checks if the dataframe is empty (has no columns).

Returns True if the dataframe has no columns, False otherwise. Note that a dataframe with columns but no rows is not considered null.

Columns

data Column Source #

Type-erased column GADT. Pattern-matching on the constructor recovers the representation; nullability is an optional bit-packed Bitmap (Nothing = no nulls, Just bm = bit i set iff row i is valid).

Instances

Instances details
Show Column Source # 
Instance details

Defined in DataFrame.Internal.Column

Eq Column Source # 
Instance details

Defined in DataFrame.Internal.Column

Methods

(==) :: Column -> Column -> Bool #

(/=) :: Column -> Column -> Bool #

fromList :: (Columnable a, ColumnifyRep (KindOf a) a) => [a] -> Column Source #

O(n) Convert a list to a column. Automatically picks the best representation of a vector to store the underlying data in.

Examples:

> fromList [(1 :: Int), 2, 3, 4]
[1,2,3,4]

fromVector :: (Columnable a, ColumnifyRep (KindOf a) a) => Vector a -> Column Source #

O(n) Convert a vector to a column. Automatically picks the best representation of a vector to store the underlying data in.

Examples:

> import qualified Data.Vector as V
> fromVector (VB.fromList [(1 :: Int), 2, 3, 4])
[1,2,3,4]

fromUnboxedVector :: (Columnable a, Unbox a) => Vector a -> Column Source #

O(n) Convert an unboxed vector to a column. This avoids the extra conversion if you already have the data in an unboxed vector.

Examples:

> import qualified Data.Vector.Unboxed as V
> fromUnboxedVector (VB.fromList [(1 :: Int), 2, 3, 4])
[1,2,3,4]

mkRandom :: (RandomGen g, Columnable a, ColumnifyRep (KindOf a) a, UniformRange a) => g -> Int -> a -> a -> Column Source #

O(n) Create a column of random elements within a range.

Takes a random number generator, a length, and a lower and upper bound for the random values.

Examples:

> import System.Random (mkStdGen)
> mkRandom (mkStdGen 42) 4 0 10
[4,2,6,5]

toList :: Columnable a => Column -> [a] Source #

O(n) Converts a column to a list. Throws an exception if the wrong type is specified.

Examples:

> column = fromList [(1 :: Int), 2, 3, 4]
> toList Int column
[1,2,3,4]
> toList Double column
exception: ...

toVector :: forall a v. (Vector v a, Columnable a) => Column -> Either DataFrameException (v a) Source #

Type-safe conversion of a column to a vector of element type a (specify via type application); Left TypeMismatchException when the column's type differs.

>>> toVector @Int @VU.Vector column
Right (unboxed vector of Ints)
>>> toVector @Text @VB.Vector column
Right (boxed vector of Text)

hasElemType :: Columnable a => Column -> Bool Source #

Whether the column stores element type a. For nullable columns, also True when a = Maybe b and the column stores b internally.

hasMissing :: Column -> Bool Source #

Checks if a column contains missing values (has a bitmap).

isNumeric :: Column -> Bool Source #

Checks if a column contains numeric values.

Element constraints

type Columnable a = (Columnable' a, ColumnifyRep (KindOf a) a, UnboxIf a, IntegralIf a, FloatingIf a, SBoolI (Unboxable a), SBoolI (Numeric a), SBoolI (IntegralTypes a), SBoolI (FloatingTypes a)) Source #

Constraint synonym for what we can put into columns.

type Columnable' a = (Typeable a, Show a, Eq a) Source #

Rows

type Row = Vector Any Source #

data Any Source #

Instances

Instances details
Show Any Source # 
Instance details

Defined in DataFrame.Internal.Row

Methods

showsPrec :: Int -> Any -> ShowS #

show :: Any -> String #

showList :: [Any] -> ShowS #

Eq Any Source # 
Instance details

Defined in DataFrame.Internal.Row

Methods

(==) :: Any -> Any -> Bool #

(/=) :: Any -> Any -> Bool #

toAny :: Columnable a => a -> Any Source #

Wraps a value into an Any type. This helps up represent rows as heterogenous lists.

fromAny :: Columnable a => Any -> Maybe a Source #

Unwraps a value from an Any type. A Null cell yields Nothing.

rowValue :: Expr a -> [(Text, Any)] -> Maybe a Source #

Given a row gets the value associated with a field.

Examples

Expand
>>> map (rowValue (F.col @Int "age")) (toRowList df)
[25,30, ...]

toRowList :: DataFrame -> [[(Text, Any)]] Source #

Convert the whole dataframe to a list of rows, one per row index in natural order; each row lists all columns ordered by column index. Materializes every row, so prefer toRowVector for large frames.

>>> toRowList df
[[("name", "Alice"), ("age", 25), ...], [("name", "Bob"), ("age", 30), ...], ...]

toRowVector :: [Text] -> DataFrame -> Vector Row Source #

Convert the dataframe to a vector of rows containing only the named columns, in the given order. An empty name list yields one empty row per dataframe row.

>>> toRowVector ["name", "age"] df
Vector of rows with only name and age fields

Expressions

data Expr a Source #

Instances

Instances details
ToTExpr cols (Expr r) Source # 
Instance details

Defined in DataFrame.Typed.Types

Methods

toTExpr :: Expr r -> AsTExpr cols (Expr r) Source #

(IsString a, Columnable a) => IsString (Expr a) Source # 
Instance details

Defined in DataFrame.Internal.Expression

Methods

fromString :: String -> Expr a #

(Floating a, Columnable a) => Floating (Expr a) Source # 
Instance details

Defined in DataFrame.Internal.Expression

Methods

pi :: Expr a #

exp :: Expr a -> Expr a #

log :: Expr a -> Expr a #

sqrt :: Expr a -> Expr a #

(**) :: Expr a -> Expr a -> Expr a #

logBase :: Expr a -> Expr a -> Expr a #

sin :: Expr a -> Expr a #

cos :: Expr a -> Expr a #

tan :: Expr a -> Expr a #

asin :: Expr a -> Expr a #

acos :: Expr a -> Expr a #

atan :: Expr a -> Expr a #

sinh :: Expr a -> Expr a #

cosh :: Expr a -> Expr a #

tanh :: Expr a -> Expr a #

asinh :: Expr a -> Expr a #

acosh :: Expr a -> Expr a #

atanh :: Expr a -> Expr a #

log1p :: Expr a -> Expr a #

expm1 :: Expr a -> Expr a #

log1pexp :: Expr a -> Expr a #

log1mexp :: Expr a -> Expr a #

(Num a, Columnable a) => Num (Expr a) Source # 
Instance details

Defined in DataFrame.Internal.Expression

Methods

(+) :: Expr a -> Expr a -> Expr a #

(-) :: Expr a -> Expr a -> Expr a #

(*) :: Expr a -> Expr a -> Expr a #

negate :: Expr a -> Expr a #

abs :: Expr a -> Expr a #

signum :: Expr a -> Expr a #

fromInteger :: Integer -> Expr a #

(Fractional a, Columnable a) => Fractional (Expr a) Source # 
Instance details

Defined in DataFrame.Internal.Expression

Methods

(/) :: Expr a -> Expr a -> Expr a #

recip :: Expr a -> Expr a #

fromRational :: Rational -> Expr a #

Show a => Show (Expr a) Source # 
Instance details

Defined in DataFrame.Internal.Expression

Methods

showsPrec :: Int -> Expr a -> ShowS #

show :: Expr a -> String #

showList :: [Expr a] -> ShowS #

prettyPrint :: Expr a -> String Source #

Render an expression as readable, width-aware pseudo-code at the default width (defaultWidth). See prettyPrintWidth to control wrapping.

prettyPrintWidth :: Int -> Expr a -> String Source #

Render an expression as readable, width-aware pseudo-code: long binary chains wrap onto aligned continuation lines, ifthenelse break onto their own lines (nested else if form a flat ladder), and sub-exprs are parenthesized by precedence.

Rendering & serialization

data TruncateConfig Source #

Configures how a DataFrame is rendered as text: maxRows caps rendered rows, maxColumns collapses middle columns past the limit into an ellipsis, and maxCellWidth truncates long cells. A non-positive field means "no limit".

Constructors

TruncateConfig 

defaultTruncateConfig :: TruncateConfig Source #

Sensible defaults for GHCi: 20 rows, 10 columns, 30 characters per cell.

toCsv :: DataFrame -> Text Source #

Convert a DataFrame to a CSV (comma-separated) text.

toCsv' :: DataFrame -> String Source #

Convert a DataFrame to a CSV (comma-separated) string.

toSeparated :: Char -> DataFrame -> Text Source #

Convert a DataFrame to a text representation with a custom separator.

toMarkdown :: DataFrame -> Text Source #

For showing the dataframe as markdown in notebooks.

toMarkdown' :: DataFrame -> String Source #

For showing the dataframe as a string markdown in notebooks.