Giggly.DataFrame behaviour (giggly v0.2.0)

Copy Markdown View Source

A table of named columns of equal length, as Giggly holds one: the data a plot was given, and each table Giggly.layer_data/2 returns, and the table a stat computes on. names/1, row_count/1, dtypes/1 and pull/2 read it; new/1, from_table/1 and put/3 build one; select/2, slice/2, split/2, sort/2, distinct/1, filter/2, mutate/2, summarise/3 and join/3 compute one from another. None takes an Elixir function, so an engine could run every one: code that needs a function pulls a column into a list and puts the result back.

Column names

The data a plot is given is read once, when the plot is created (Giggly.Plot.new/2), and so is a layer's own table, by its function's data: option; their column names become strings whatever the source used. Every column Giggly computes is named by an atom, such as the aesthetic :x, so a user column called "x" can never be mistaken for one.

Values

A column is a list of values in row order, stored as given, but for one conversion its type makes: an integer in a column of floats becomes a float, as Explorer casts one, so 4 and 4.0 are the same value. nil is a missing value, which is how every Table.Reader source returns a null.

Types

Every column has one type, which dtypes/1 reports. It is worked out from the values when the column enters a frame, by Explorer's rules for building a series from a list:

  • the non-missing values are all of one type, and a column mixing two types is refused with an ArgumentError naming the column and two values that disagree;
  • integers and floats together are :float, the integers stored as floats, and :nan, :infinity and :neg_infinity are floats, as Explorer gives a non-finite float;
  • a column of only nil, or of no rows, is :null;
  • binaries are :string;
  • Date, Time and NaiveDateTime are :date, :time and :naive_datetime, and a DateTime is {:datetime, time_zone}, its time zone's name, as Explorer types one, so date-times in two zones are two types ("Etc/UTC" and "Europe/London") and a column mixing them is refused.

A column of lists is {:list, type}, the type its lists' values have, worked out as a column's is from all of them together, so [[1, 2.0], []] is {:list, :float}, its integers stored as floats, a column holding only empty lists is {:list, :null}, a list of lists is a list of a list type, and lists whose values mix two types are refused. A missing list, or a missing value in one, is nil.

Two types exist here and not in Explorer, since a list holds Elixir values an engine cannot: :atom, for any other atom, and :term, for anything else (maps, tuples, Decimal and other structs). A :term column's values are not checked against each other; it is the frame's equivalent of pandas' and Polars' object type, so a column the plot never uses cannot refuse the table.

One more is never worked out from values, only declared when a column is built, or read from a table whose column is a Giggly.Factor: :category, Explorer's name for a categorical column, which a plot reads as discrete whatever its values are. Its values are typed, and an integer among floats becomes a float, as any column's; it may hold anything but a :term's values or lists. A category has levels, the values it may hold in their order, given when it is declared or else its distinct values in order (numbers by value, minus infinity first and NaN last, dates and times by time), and it may be declared ordered; a value not among its levels is missing.

Expressions

What filter/2 and mutate/2 compute in each row is an expression, plain data rather than a function, so an engine can run it:

  • a column, by its name, an atom or a string: :x, "cyl"; true, false and nil name none;
  • a constant, the value in every row: a number, a boolean, a date or a time written alone, or any value but a list as {:constant, term}, typed as a column holding it would be;
  • an operation, {name, [expression, ...]}, or {name, [expression, ...], options} for parameters that are not row values:
    • {:is_finite, [x]}, whether each number is finite, a boolean;
    • {:is_missing, [x]}, whether each value of any type is missing, nil or NaN, as R's is.na() reads one, a boolean never itself missing;
    • {:and, [left, right]}, {:or, [left, right]} and {:not, [x]}, a boolean;
    • {:<, [left, right]} and {:>, [left, right]}, whether one number is below or above another, a boolean;
    • {:log10, [x]}, a number's base-10 logarithm, a float, as R's log10() gives it, zero's minus infinity and a negative number's NaN;
    • {:floor, [x]}, the greatest whole number at or below a float, a float;
    • {:if_else, [condition, when_true, when_false]}, each row's when_true where its condition is true and its when_false where it is false or missing, both numbers, an integer where both are and a float otherwise;
    • {:cut, [x], breaks: breaks, closed: :right}, the interval of breaks, ascending, each number falls in, numbered from 1, an integer, as R's cut(include.lowest = TRUE) numbers it: closed on the right, or on the left with closed: :left, and the outermost interval at its outer end too, a number outside them all missing;
    • {:+, [left, right]}, {:-, [left, right]}, {:*, [left, right]} and {:/, [left, right]}, as IEEE arithmetic gives them: an integer where both are integers and the operation is not a division, and a float otherwise.

Values follow Explorer's semantics, as types do, but for is_missing, which reads a missing value rather than passing it on: a missing value passes through an operation as missing, so is_finite is missing where its number is; and is false where either side is false, true where both are true and missing otherwise, and or true where either side is true, false where both are false and missing otherwise, SQL's three-valued logic; a comparison is missing where either number is, and puts NaN above every number, both infinities included; arithmetic is missing where either number is. An expression outside this vocabulary, or an operation given a column of the wrong type, whether or not the frame has rows, raises FunctionClauseError. A frame names the operations its engine runs (operations/1), so a caller can refuse one it cannot run before running it.

iex> frame = Giggly.DataFrame.new(x: [1.0, 4.0, 9.0])
iex> frame
...> |> Giggly.DataFrame.mutate(big: {:>, [:x, 3]}, half: {:/, [:x, 2]})
...> |> Giggly.DataFrame.filter(:big)
...> |> Giggly.DataFrame.pull(:half)
[2.0, 4.5]

Implementations

This module is both the only module callers use and the behaviour an implementation of a frame follows: each function here dispatches on the frame's struct to the module that defines it, which adopts the callbacks below, one per function. new/1 and from_table/1 build Giggly's own, which holds every column as a list; another implementation, an engine's, builds frames of its own struct, and every function here then runs on them. An implementation names the operations and aggregates it runs (operations/1).

Summary

Types

What summarise/3 computes over each group's values of a column.

How a column is declared a category: :category, its levels its distinct values in order, or {:category, levels: levels, ordered: ordered}, either option left out to take that default (ordered: false).

A column as built: its name and values, and how it is declared a category if it is one.

A column's type, as the moduledoc's Types section describes.

What a frame computes in each row: a column by its name, a constant, or an operation on expressions by its name.

A column name: a string for a column read from the input, an atom for every other.

t()

A frame: Giggly's own, or an implementation's, read through the functions here.

Callbacks

A category's declaration, as category/2 describes.

The rows of several frames, one after another, as concat_rows/1 describes.

Each row once, as distinct/1 describes.

Each column's type, as dtypes/1 describes.

The rows an expression is true in, as filter/2 describes.

The rows of one frame joined with those of another, as join/3 describes.

The same table, made concrete where it is held, as materialize/1 describes.

Adds or replaces columns computed from expressions, as mutate/2 describes.

The column names, as names/1 describes.

Builds a frame from columns, as new/1 describes.

The operations its expressions and aggregates may name, as operations/1 describes.

One column's values, as pull/2 describes.

Adds or replaces a column, as put/3 describes.

Adds or replaces a column declared a category, as put/4 describes.

The number of rows, as row_count/1 describes.

Chooses and renames columns, as select/2 describes.

The rows at the given indices, as slice/2 describes.

The rows in order of the given columns, as sort/2 describes.

The rows split by the values of one column, as split/2 describes.

One row per group of rows, as summarise/3 describes.

The same table as Giggly's own frame, as to_lists/1 describes.

Functions

How the category name is declared, its levels and whether it is ordered, as put/4 would declare it again: {:category, levels: levels, ordered: ordered}. Raises FunctionClauseError for a column that is not a category.

The rows of frames, one frame after another, each frame's rows in their order, as Explorer's concat_rows/1 and dplyr's bind_rows() stack them: so a computation done a group at a time joins its parts. The frames hold the same columns, in any order, the first frame's order kept, and each column one type in every frame: integers beside floats are floats, a column of missing values alone takes the other frames' type, and a category's levels are every frame's, typed together as a column's values are, in the order each first appears, an ordered category's only where every frame's is ordered with the same levels. A frame held by another implementation than the first is read into the first's. Frames holding other columns, a column of two other types, or ordered categories whose levels differ raise ArgumentError, naming the column.

The frame's rows with each row a later one repeats left out, in the order they first appear, as vctrs' vec_unique() and Explorer's distinct/2 keep them: two rows are one where every column holds equal values, a missing value equal to a missing one and NaN to NaN, never to each other, and -0.0 to 0.0, the first kept as it is. Every column and each column's type are kept.

Each column's type, by column name, as the moduledoc's Types section describes.

The frame's rows in which expression is true, in their order, every column keeping its type, as dplyr's filter() and SQL's WHERE keep them: a row where it is false or missing is left out. The expression is a boolean one, as the Expressions section describes.

Reads any Table.Reader (a list of maps, a map of columns, an Explorer data frame, a database result) into a frame, as a plot reads its data.

The rows of left joined with the rows of right whose columns on hold the same values, as dplyr's left_join(by = ) and SQL's LEFT JOIN join them: each left row, in order, once for each matching right row, in right's order, with right's other columns after left's, or once with those columns missing where no right row matches. A missing value in a column on matches nothing, and -0.0 matches 0.0 and NaN matches NaN, as Explorer joins. Each column keeps its type, a category's included. right may be held by another implementation than left, as a small table built in lists may be joined to one an engine holds: it is then read into left's. A column on that either frame lacks, two of them of different types, and a column besides those on in both frames raise FunctionClauseError.

The same table, made concrete where its implementation holds it, so that every later operation and read starts from its rows rather than from the work that made them. An implementation that defers its operations until a result is read, as an engine's lazy frame does, works them out once here and keeps the rows where it holds them, as an engine's own table; one that computes each operation as it is called, Giggly's own included, returns the frame as it is. Unlike to_lists/1, it never moves the rows out of the implementation.

The frame with each of columns, a {name, expression}, computed in every row: replaced where it is, or added after the last column, in the order given, as dplyr's mutate() adds them. Every expression reads the frame as given, never a column the same call computes, as Polars' with_columns() reads. A column named alone keeps its type, a category's included; a constant's type is worked out from its value, and an operation's is what it computes.

The column names, in column order.

A frame of the columns columns, each a {name, values} pair, in the order given: values a list of one value per row, its type worked out as the Types section says. A column given as {name, values, category} is declared a category (category/0).

The operations an expression may name that the frame's implementation runs, as the Expressions section describes them, and the aggregates summarise/3 may compute: Giggly's own frame runs every one, and an engine those it can.

One column's values, as a list in row order. Raises FunctionClauseError when the frame has no column of that name.

The frame with the column name holding values, a list of one value per row: replaced where it is, or added after the last column. Its type is worked out from values, so a replacement may change it.

The frame with the column name holding values, declared a category, as new/1 declares a column given as {name, values, category}: replaced where it is, or added after the last column. Raises as put/3 does, and ArgumentError as new/1 does for a category.

The number of rows.

A frame of the columns named in renames, each {new_name, existing_name}, in that order, as dplyr's select(x = wt) chooses them: an existing column may be chosen under several new names, and any column not named is dropped. The rows are kept even when no column is.

The frame's rows at indices, counting from 0, in the order given, as vctrs' vec_slice() takes them: a row may be taken more than once or not at all. Every column and each column's type are kept. Raises ArgumentError for an index past the last row.

The frame's rows in order of the columns names, each of numbers, the first deciding and each next one breaking its ties, rows tied in every one kept in their order, as R's order() sorts, stably: minus infinity first, then the numbers, infinity, and a missing value or NaN last, alike. Every column and each column's type are kept. Raises FunctionClauseError for a column that is not of numbers.

The frame's rows split by the values of the column name, as R's split() splits a table: for each value the column holds, a frame of the rows holding it, in their order, with every column and each column's type, -0.0 and 0.0 one value, keyed 0.0. A value the column does not hold has no frame.

One row for each combination of the values of the columns by that the frame's rows hold, as dplyr's summarise(.by = ) and SQL's GROUP BY give one, with each of aggregates, a {name, aggregate} in order, computed over that group's rows. The by columns come first, in the order given, each keeping its type, a category's included; each aggregate follows as a column of its own. The rows are sorted by the by columns, the first deciding first, each by its values in Erlang's term order with a missing one last, as SQL's NULLS LAST orders them.

The same table as Giggly's own frame, which holds every column as a list in Elixir's memory, as new/1 builds one: each column's values, in their order, and its type, a category's levels and ordered flag included. A frame that is Giggly's own already is returned as it is; an engine's is read out of the engine, so code that works on lists can take any frame.

Types

aggregate()

@type aggregate() ::
  {:sum, [name()]}
  | {:sum, [name()], summation: :extended | :plain, missing: :propagate | :skip}
  | {:mean, [name()]}
  | {:variance, [name()]}
  | {:n_distinct, [name()]}
  | {:first, [name()]}
  | {:quantile, [name()], [{:probability, number()}]}
  | {:count, [name()]}
  | {:min, [name()]}
  | {:max, [name()]}
  | {:list, [name()]}

What summarise/3 computes over each group's values of a column.

category()

@type category() :: :category | {:category, levels: list(), ordered: boolean()}

How a column is declared a category: :category, its levels its distinct values in order, or {:category, levels: levels, ordered: ordered}, either option left out to take that default (ordered: false).

column()

@type column() :: {name(), list()} | {name(), list(), category()}

A column as built: its name and values, and how it is declared a category if it is one.

dtype()

@type dtype() ::
  :integer
  | :float
  | :string
  | :boolean
  | :atom
  | :date
  | :time
  | :naive_datetime
  | {:datetime, String.t()}
  | :null
  | :term
  | :category
  | {:list, dtype()}

A column's type, as the moduledoc's Types section describes.

expression()

@type expression() ::
  name()
  | number()
  | boolean()
  | Date.t()
  | Time.t()
  | NaiveDateTime.t()
  | DateTime.t()
  | {:constant, term()}
  | {:is_finite, [expression()]}
  | {:is_missing, [expression()]}
  | {:and, [expression()]}
  | {:or, [expression()]}
  | {:not, [expression()]}
  | {:<, [expression()]}
  | {:>, [expression()]}
  | {:log10, [expression()]}
  | {:floor, [expression()]}
  | {:if_else, [expression()]}
  | {:cut, [expression()], breaks: [number()], closed: :right | :left}

What a frame computes in each row: a column by its name, a constant, or an operation on expressions by its name.

name()

@type name() :: atom() | String.t()

A column name: a string for a column read from the input, an atom for every other.

t()

@opaque t()

A frame: Giggly's own, or an implementation's, read through the functions here.

Callbacks

category(t, name)

@callback category(t(), name()) :: {:category, levels: list(), ordered: boolean()}

A category's declaration, as category/2 describes.

concat_rows(list)

@callback concat_rows([t(), ...]) :: t()

The rows of several frames, one after another, as concat_rows/1 describes.

distinct(t)

@callback distinct(t()) :: t()

Each row once, as distinct/1 describes.

dtypes(t)

@callback dtypes(t()) :: %{optional(name()) => dtype()}

Each column's type, as dtypes/1 describes.

filter(t, expression)

@callback filter(t(), expression()) :: t()

The rows an expression is true in, as filter/2 describes.

join(t, t, list)

@callback join(t(), t(), [name(), ...]) :: t()

The rows of one frame joined with those of another, as join/3 describes.

materialize(t)

@callback materialize(t()) :: t()

The same table, made concrete where it is held, as materialize/1 describes.

mutate(t, list)

@callback mutate(t(), [{name(), expression()}]) :: t()

Adds or replaces columns computed from expressions, as mutate/2 describes.

names(t)

@callback names(t()) :: [name()]

The column names, as names/1 describes.

new(list)

@callback new([column()]) :: t()

Builds a frame from columns, as new/1 describes.

operations(t)

@callback operations(t()) :: [atom()]

The operations its expressions and aggregates may name, as operations/1 describes.

pull(t, name)

@callback pull(t(), name()) :: list()

One column's values, as pull/2 describes.

put(t, name, list)

@callback put(t(), name(), list()) :: t()

Adds or replaces a column, as put/3 describes.

put(t, name, list, category)

@callback put(t(), name(), list(), category()) :: t()

Adds or replaces a column declared a category, as put/4 describes.

row_count(t)

@callback row_count(t()) :: non_neg_integer()

The number of rows, as row_count/1 describes.

select(t, list)

@callback select(t(), [{name(), name()}]) :: t()

Chooses and renames columns, as select/2 describes.

slice(t, list)

@callback slice(t(), [non_neg_integer()]) :: t()

The rows at the given indices, as slice/2 describes.

sort(t, list)

@callback sort(t(), [name(), ...]) :: t()

The rows in order of the given columns, as sort/2 describes.

split(t, name)

@callback split(t(), name()) :: %{optional(term()) => t()}

The rows split by the values of one column, as split/2 describes.

summarise(t, list, list)

@callback summarise(t(), [name(), ...], [{name(), aggregate()}]) :: t()

One row per group of rows, as summarise/3 describes.

to_lists(t)

@callback to_lists(t()) :: t()

The same table as Giggly's own frame, as to_lists/1 describes.

Functions

category(frame, name)

@spec category(t(), name()) :: {:category, levels: list(), ordered: boolean()}

How the category name is declared, its levels and whether it is ordered, as put/4 would declare it again: {:category, levels: levels, ordered: ordered}. Raises FunctionClauseError for a column that is not a category.

iex> [size: ["m", "s"]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.put(:size, ["m", "s"], {:category, levels: ["s", "m"]})
...> |> Giggly.DataFrame.category(:size)
{:category, levels: ["s", "m"], ordered: false}

concat_rows(frames)

@spec concat_rows([t(), ...]) :: t()

The rows of frames, one frame after another, each frame's rows in their order, as Explorer's concat_rows/1 and dplyr's bind_rows() stack them: so a computation done a group at a time joins its parts. The frames hold the same columns, in any order, the first frame's order kept, and each column one type in every frame: integers beside floats are floats, a column of missing values alone takes the other frames' type, and a category's levels are every frame's, typed together as a column's values are, in the order each first appears, an ordered category's only where every frame's is ordered with the same levels. A frame held by another implementation than the first is read into the first's. Frames holding other columns, a column of two other types, or ordered categories whose levels differ raise ArgumentError, naming the column.

iex> first = Giggly.DataFrame.new(group: [1, 1], x: [1, 2])
iex> second = Giggly.DataFrame.new(x: [3.5], group: [2])
iex> joined = Giggly.DataFrame.concat_rows([first, second])
iex> {Giggly.DataFrame.names(joined), Giggly.DataFrame.pull(joined, :x)}
{[:group, :x], [1.0, 2.0, 3.5]}

iex> first = Giggly.DataFrame.new([{:fill, ["b", "a"], :category}])
iex> second = Giggly.DataFrame.new([{:fill, ["c", "a"], :category}])
iex> [first, second]
...> |> Giggly.DataFrame.concat_rows()
...> |> Giggly.DataFrame.category(:fill)
{:category, levels: ["a", "b", "c"], ordered: false}

distinct(frame)

@spec distinct(t()) :: t()

The frame's rows with each row a later one repeats left out, in the order they first appear, as vctrs' vec_unique() and Explorer's distinct/2 keep them: two rows are one where every column holds equal values, a missing value equal to a missing one and NaN to NaN, never to each other, and -0.0 to 0.0, the first kept as it is. Every column and each column's type are kept.

iex> [x: [1, 2, 1], y: ["a", "b", "a"]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.distinct()
...> |> Giggly.DataFrame.pull(:x)
[1, 2]

dtypes(frame)

@spec dtypes(t()) :: %{optional(name()) => dtype()}

Each column's type, by column name, as the moduledoc's Types section describes.

iex> frame =
...>   %{"cyl" => ["6", "4"], "mpg" => [21.0, 22.8]}
...>   |> Giggly.Plot.new(%{x: :cyl, y: :mpg})
...>   |> Giggly.Plot.geom_point()
...>   |> Giggly.layer_data()
iex> Giggly.DataFrame.dtypes(frame)
%{
  x: :integer,
  y: :float,
  panel: :integer,
  group: :integer,
  shape: :atom,
  color: :string,
  fill: :null,
  size: :float,
  alpha: :null,
  stroke: :float
}

filter(frame, expression)

@spec filter(t(), expression()) :: t()

The frame's rows in which expression is true, in their order, every column keeping its type, as dplyr's filter() and SQL's WHERE keep them: a row where it is false or missing is left out. The expression is a boolean one, as the Expressions section describes.

iex> [x: [1.0, nil, 5.0]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.filter({:>, [:x, 2]})
...> |> Giggly.DataFrame.pull(:x)
[5.0]

from_table(table)

@spec from_table(Table.Reader.t()) :: t()

Reads any Table.Reader (a list of maps, a map of columns, an Explorer data frame, a database result) into a frame, as a plot reads its data.

An Explorer data frame, eager or lazy, stays in Explorer where the project lists it, its rows read only when they are needed. Each column is of the type Explorer declares it: integers of any width, floats, a category's values as strings, a binary column's as strings, dates and times, a date-time to the microsecond, lists of these, a column of only missing values as its declared type; a decimal, a struct or a duration column, or lists of them, is :term, its values as Explorer gives them. Only two kinds of column are read here, to check that their values fit: unsigned 64-bit integers, which must not pass the largest signed one, and binaries, which must be UTF-8 text.

Every other table's columns are read into lists, so a lazy source is read here and nowhere else, and column names become strings. A list of maps gives its columns in sorted key order, since table sorts the first row's keys; a list of keyword lists keeps its written order. A column given as a Giggly.Factor is a category with the factor's levels and ordered flag.

Raises ArgumentError when the input is a list or map that is not a table, when two of its names become the same string (:wt and "wt"), when its columns differ in length, when a column mixes two types, for a factor new/1 would refuse as a category, or for an Explorer column whose values do not fit as above; a value with no Table.Reader implementation raises Protocol.UndefinedError.

iex> frame = Giggly.DataFrame.from_table([%{wt: 2.62, cyl: "6"}, %{wt: 2.875, cyl: "4"}])
iex> Giggly.DataFrame.names(frame)
["cyl", "wt"]

join(left, right, on)

@spec join(t(), t(), [name(), ...]) :: t()

The rows of left joined with the rows of right whose columns on hold the same values, as dplyr's left_join(by = ) and SQL's LEFT JOIN join them: each left row, in order, once for each matching right row, in right's order, with right's other columns after left's, or once with those columns missing where no right row matches. A missing value in a column on matches nothing, and -0.0 matches 0.0 and NaN matches NaN, as Explorer joins. Each column keeps its type, a category's included. right may be held by another implementation than left, as a small table built in lists may be joined to one an engine holds: it is then read into left's. A column on that either frame lacks, two of them of different types, and a column besides those on in both frames raise FunctionClauseError.

iex> left = Giggly.DataFrame.new(g: ["a", "b"], x: [1, 2])
iex> right = Giggly.DataFrame.new(g: ["a"], label: ["first"])
iex> left |> Giggly.DataFrame.join(right, [:g]) |> Giggly.DataFrame.pull(:label)
["first", nil]

materialize(frame)

@spec materialize(t()) :: t()

The same table, made concrete where its implementation holds it, so that every later operation and read starts from its rows rather than from the work that made them. An implementation that defers its operations until a result is read, as an engine's lazy frame does, works them out once here and keeps the rows where it holds them, as an engine's own table; one that computes each operation as it is called, Giggly's own included, returns the frame as it is. Unlike to_lists/1, it never moves the rows out of the implementation.

iex> frame = Giggly.DataFrame.new(x: [1, 2.5])
iex> Giggly.DataFrame.materialize(frame) == frame
true

mutate(frame, columns)

@spec mutate(t(), [{name(), expression()}]) :: t()

The frame with each of columns, a {name, expression}, computed in every row: replaced where it is, or added after the last column, in the order given, as dplyr's mutate() adds them. Every expression reads the frame as given, never a column the same call computes, as Polars' with_columns() reads. A column named alone keeps its type, a category's included; a constant's type is worked out from its value, and an operation's is what it computes.

iex> [x: [1, 2]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.mutate(y: {:*, [:x, 10]}, x: {:+, [:x, 0.5]})
...> |> then(fn frame -> {Giggly.DataFrame.pull(frame, :x), Giggly.DataFrame.pull(frame, :y)} end)
{[1.5, 2.5], [10, 20]}

names(frame)

@spec names(t()) :: [name()]

The column names, in column order.

new(columns)

@spec new([column()]) :: t()

A frame of the columns columns, each a {name, values} pair, in the order given: values a list of one value per row, its type worked out as the Types section says. A column given as {name, values, category} is declared a category (category/0).

Raises FunctionClauseError when the lists differ in length or a name appears twice, and ArgumentError when a column mixes two types, or for a category of values with no type of their own, or whose levels repeat a value (4 and 4.0 are one), hold nil, or are of another type than its values.

iex> frame = Giggly.DataFrame.new(x: [1, 2.5], label: ["a", "b"])
iex> Giggly.DataFrame.names(frame)
[:x, :label]
iex> Giggly.DataFrame.pull(frame, :x)
[1.0, 2.5]

operations(frame)

@spec operations(t()) :: [atom()]

The operations an expression may name that the frame's implementation runs, as the Expressions section describes them, and the aggregates summarise/3 may compute: Giggly's own frame runs every one, and an engine those it can.

pull(frame, name)

@spec pull(t(), name()) :: list()

One column's values, as a list in row order. Raises FunctionClauseError when the frame has no column of that name.

put(frame, name, values)

@spec put(t(), name(), list()) :: t()

The frame with the column name holding values, a list of one value per row: replaced where it is, or added after the last column. Its type is worked out from values, so a replacement may change it.

Raises FunctionClauseError unless values has one value per row, and ArgumentError when they mix two types.

iex> frame =
...>   [x: [1.0, 2.0]]
...>   |> Giggly.DataFrame.new()
...>   |> Giggly.DataFrame.put(:y, [3, 4])
iex> Giggly.DataFrame.dtypes(frame)
%{x: :float, y: :integer}

put(frame, name, values, category)

@spec put(t(), name(), list(), category()) :: t()

The frame with the column name holding values, declared a category, as new/1 declares a column given as {name, values, category}: replaced where it is, or added after the last column. Raises as put/3 does, and ArgumentError as new/1 does for a category.

row_count(frame)

@spec row_count(t()) :: non_neg_integer()

The number of rows.

select(frame, renames)

@spec select(t(), [{name(), name()}]) :: t()

A frame of the columns named in renames, each {new_name, existing_name}, in that order, as dplyr's select(x = wt) chooses them: an existing column may be chosen under several new names, and any column not named is dropped. The rows are kept even when no column is.

iex> [wt: [2.62], cyl: ["6"]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.select(x: :wt, weight: :wt)
...> |> Giggly.DataFrame.names()
[:x, :weight]

slice(frame, indices)

@spec slice(t(), [non_neg_integer()]) :: t()

The frame's rows at indices, counting from 0, in the order given, as vctrs' vec_slice() takes them: a row may be taken more than once or not at all. Every column and each column's type are kept. Raises ArgumentError for an index past the last row.

iex> [y: [3, 4, 5]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.slice([2, 0, 0])
...> |> Giggly.DataFrame.pull(:y)
[5, 3, 3]

sort(frame, names)

@spec sort(t(), [name(), ...]) :: t()

The frame's rows in order of the columns names, each of numbers, the first deciding and each next one breaking its ties, rows tied in every one kept in their order, as R's order() sorts, stably: minus infinity first, then the numbers, infinity, and a missing value or NaN last, alike. Every column and each column's type are kept. Raises FunctionClauseError for a column that is not of numbers.

iex> [x: [3, nil, 1], y: ["c", "missing", "a"]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.sort([:x])
...> |> Giggly.DataFrame.pull(:y)
["a", "c", "missing"]

split(frame, name)

@spec split(t(), name()) :: %{optional(term()) => t()}

The frame's rows split by the values of the column name, as R's split() splits a table: for each value the column holds, a frame of the rows holding it, in their order, with every column and each column's type, -0.0 and 0.0 one value, keyed 0.0. A value the column does not hold has no frame.

iex> [panel: [1, 2, 1], y: [3, 4, 5]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.split(:panel)
...> |> Map.new(fn {panel, rows} -> {panel, Giggly.DataFrame.pull(rows, :y)} end)
%{1 => [3, 5], 2 => [4]}

summarise(frame, by, aggregates)

@spec summarise(t(), [name(), ...], [{name(), aggregate()}]) :: t()

One row for each combination of the values of the columns by that the frame's rows hold, as dplyr's summarise(.by = ) and SQL's GROUP BY give one, with each of aggregates, a {name, aggregate} in order, computed over that group's rows. The by columns come first, in the order given, each keeping its type, a category's included; each aggregate follows as a column of its own. The rows are sorted by the by columns, the first deciding first, each by its values in Erlang's term order with a missing one last, as SQL's NULLS LAST orders them.

An aggregate is an operation, {name, [column]} or {name, [column], options}, over each group's values of one column, in row order:

  • {:n_distinct, [column]}, how many distinct values the group holds, an integer, a missing value counting as one, as R's vec_unique_count() and Polars' n_unique() count it;
  • {:first, [column]}, the group's first value, of the column's type, a category's included;
  • {:list, [column]}, the group's values in a list, missing ones included, of the type {:list, type}, the column's type;
  • {:count, [column]}, how many of the group's values are not missing, an integer;
  • {:min, [column]} and {:max, [column]}, the least and the greatest of a column of numbers, of its type, an infinity below or above every number;
  • {:quantile, [column], probability: probability}, of a column of numbers at a probability from 0 to 1, a float, as R's quantile() takes it by default (type 7, SQL's percentile_cont): the numbers sorted and the one at the place 1 + (n - 1) * probability, counting from 1, or where the place falls between two that differ, (1 - h) * lower + h * higher for the fraction h of the way, in that order, as R works it;
  • {:sum, [column]}, the sum of a column of numbers, a float, which takes two options;
  • {:mean, [column]}, the mean of a column of numbers, a float, as R's mean() works it out in a long double digit for digit, missing where the group holds a nil (R's NA), else NaN where it holds NaN or both infinities, else an infinity where it holds one;
  • {:variance, [column]}, the variance of a column of numbers, a float, as R's var() works it out digit for digit, missing where the group holds a missing value or fewer than two, else NaN where it holds an infinity.

count, min, max and quantile leave a missing value out, one that is nil (R's NA) or NaN, as R's is.na() reads one and as R's statistics call them, with na.rm = TRUE; a group with none is missing, or counts 0.

A sum's summation: says how it adds: :extended, the default, in R's long double, as R's sum() adds in extended precision, so the sum is the one R's gives, a sum past the largest double an infinity; :plain adds the values in row order in floats, as R's rowsum() does, an infinity from wherever the running sum passes the largest double. missing: says what a missing value does, one that is nil (R's NA) or NaN: :propagate, the default, makes the group's sum missing, nil where one is, else NaN, as R's sum() gives NA or NaN; :skip leaves it out, as rowsum(na.rm = TRUE) does, a group of only missing values summing to 0. An infinity is no missing value: it adds as IEEE adds one, so the sum is that infinity, or NaN where both meet, as R's is. A sum, a minimum, a maximum, a mean, a variance or a quantile of a column of another type and an option not listed are refused with an ArgumentError; an empty by, an option's value not listed, a quantile with no probability, an aggregate not listed and an aggregate's name shared with a by column raise FunctionClauseError.

iex> [g: ["a", "b", "a"], y: [1.0, 5.0, 3.0]]
...> |> Giggly.DataFrame.new()
...> |> Giggly.DataFrame.summarise([:g], mean: {:mean, [:y]}, rows: {:count, [:y]})
...> |> then(fn frame -> {Giggly.DataFrame.pull(frame, :g), Giggly.DataFrame.pull(frame, :mean)} end)
{["a", "b"], [2.0, 5.0]}

to_lists(frame)

@spec to_lists(t()) :: t()

The same table as Giggly's own frame, which holds every column as a list in Elixir's memory, as new/1 builds one: each column's values, in their order, and its type, a category's levels and ordered flag included. A frame that is Giggly's own already is returned as it is; an engine's is read out of the engine, so code that works on lists can take any frame.

iex> frame = Giggly.DataFrame.new(x: [1, 2.5])
iex> Giggly.DataFrame.to_lists(frame) == frame
true