module

Crysda::DataFrame

A "tabular" data structure representing cases/records (rows), each of which consists of a number of observations or measurements (columns) DataFrame is an immutable object, any mutation will return a new object.

Constants

DEF_NEST_COLUMN_NAME = "data"

Instance methods

[](name : String) : DataCol

Returns a column by name

Source
[](index : Int32)

Returns a column by index

Source
add_column(tf : ColumnFormula) : DataFrame

Adds new variables and preserves existing

Source
add_column(col_name : String, &expression : TableExpression) : DataFrame

Add a new column and preserve existing ones.

df.add_column("salary_category") { 3 }             # with constant value
df.add_column("age_3y_later") { |e| e["age"] + 3 } # by doing basic column arithmetics
Source
add_columns(cols : Iterable(ColumnFormula)) : DataFrame
Source
add_columns(*cols : ColumnFormula) : DataFrame

add multiple columns

df.add_columns(
  "age_plus3".with { |e| e["age"] + 3 },
  "initials".with { |e| e["first_name"].map(&.to_s[0]).concatenate(e["last_name"].map(&.to_s[0])) }
)
Source
add_row(*row)

Returns a DataFrame containing the new row. The new row length must match the number of columns in the DataFrame

Source
add_row_number(name = "row_number")

Add the row-number as column to data-frame

Source
apply_rows(col_name : String, &row_func : DataFrameRow -> Any) : DataFrame

Apply a function to each row, returning a new DataFrame with the result column added The block receives a DataFrameRow and should return a scalar value

df.apply_rows("total") { |row| row["price"].as_f * row["qty"].as_i }
df.apply_rows("full_name") { |row| "#{row["first"].as_s} #{row["last"].as_s}" }
Source
bind_cols(cols : Iterable(DataCol))

Add new columns. rows are matched by position, so all data frames must have the same number of rows.

Source
bind_rows(df : DataFrame) : DataFrame

Adds new rows. Missing entries are set to null. The output of bind_rows will contain a column if that column appears in any of the inputs. When row-binding, columns are matched by name, and any missing columns will be filled with NA. Grouping will be discarded when binding rows

Source
bind_rows(rows : Iterable(Hash(String, Any)))
Source
bind_rows(dfs : Iterable(DataFrame)) : DataFrame
Source
bind_rows(*rows : Hash(String, Any))

Add new rows. Missing entries are set to nil. The output of bind_rows will contain a column if that column appears in any of the inputs. when row-binding, columns are matched by name, and any missing column will be filled with NA Grouping will be discarded when binding rows

row1 = {
  "person" => "james",
  "year"   => 1996,
  "weight" => 54.0,
  "sex"    => "M",
} of String => Any

row2 = {
  "person" => "nell",
  "year"   => 1997,
  "weight" => 48.1,
  "sex"    => "F",
} of String => Any
df.bind_rows(row1, row2)
Source
bind_rows(*rows : DataFrameRow)
Source
bind_rows(*df : DataFrame) : DataFrame
Source
coalesce(*columns : String) : DataCol

Get first non-null value from multiple columns (coalesce)

Source
cols

Ordered list of column in this data-frame

Source
complete(*column_names : String) : DataFrame

Turns implicit missing values into explicit missing values. This is a wrapper around #expand

Source
count(selects : Array(String) = [] of String, name = "n") : DataFrame
Source
count(*selects : String, name = "n") : DataFrame

Counts observations by group.

If no grouping attributes are provided the method will respect the grouping of the receiver, or in cases of an ungrouped receiver will simply count the rows in the data.frame

selects : The variables to to be used for cross-tabulation. name : The name of the count column resulting table.

df.count("column name")
Source
count_expr(*exprs : TableExpression, name = "n", table_expression : TableExpression | Nil = nil) : DataFrame

Counts expressions

If no grouping attributes are provided the method will respect the grouping of the receiver, or in cases of an ungrouped receiver will simply count the rows in the data.frame

Source
describe

Describe numeric columns with summary statistics

Source
distinct(selects : Array(String) = self.names) : DataFrame
Source
distinct(*selects : String) : DataFrame

Retains only unique/distinct rows selects : Variables to use when determining uniqueness. If there are multiple rows for a given combination of inputs, only the first row will be preserved.

Source
drop_duplicates(*columns : String) : DataFrame

Drop duplicate rows

Source
dropna(columns : Array(String) | Nil = nil) : DataFrame

Drop rows containing any null values If columns are specified, only check those columns for nulls

Source
dropna(*columns : String) : DataFrame

Drop rows containing any null values in specified columns

Source
duplicated(*columns : String) : Array(Bool)

Get duplicate rows

Source
expand(*column_names : String) : DataFrame

expand is often useful in conjunction with left_join if you want to convert implicit missing values to explicit missing values.

Source
fillna(columns : Array(String), value) : DataFrame

Fill null values in specific columns

Source
fillna(values : Hash(String, _)) : DataFrame

Fill null values in specific columns using a hash of column => value

Source
fillna(value) : DataFrame

Fill null values in all columns with a default value Returns a new DataFrame with nulls replaced

Source
filter

Filter the rows of a table with a single predicate. the filter() function is used to subset a data frame, retaining all rows that satisfy your conditions.

Source
filter(*predicates : DataFrame -> Array(Bool) | Array(Bool | Nil)) : DataFrame

AND-filter a table with different filters. Subset rows with filter

df.filter { |e| e.["age"] == 23 }
df.filter { |e| e.["weight"] > 50 }
df.filter { |e| e["first_name"].matching { |e| e.starts_with?("Ho") } }
Source
filter_by_row

filter rows by Row predicate, which is invoked on each row of the dataframe

df = Crysda.dataframe_of("person", "year", "weight", "sex").values(
  "max", 2014, 33.1, "M",
  "max", 2016, nil, "M",
  "anna", 2015, 39.2, "F",
  "anna", 2016, 39.9, "F"
)
df.filter_by_row { |f| f["year"].as_i > 2015 }.print
Source
filter_by_row_with_index

filter rows by Row predicate, which is invoked on each row of the dataframe

df = Crysda.dataframe_of("person", "year", "weight", "sex").values(
  "max", 2014, 33.1, "M",
  "max", 2016, nil, "M",
  "anna", 2015, 39.2, "F",
  "anna", 2016, 39.9, "F"
)
df.filter_by_row_with_index { |f, i| f["year"].as_i > 2015 || i % 2 != 0 }.print
Source
gather(key : String, value : String, columns : Array(String) = self.names, convert : Bool = false) : DataFrame

gather takes multiple columns and collapses into key-value pairs, duplicating all other columns as needed. You use gather() when you notice that you have columns that are not variables.

key Name of the key column to create in output. value Name of the value column to create in output. columns The colums to gather. The same selectar syntax as for krangl::select is supported here convert If TRUE will automatically run convertType on the key column. This is useful if the column names are actually numeric, integer, or logical.

Source
gather(key : String, value : String, columns : ColumnSelector, convert : Bool = false) : DataFrame
Source
group_by(by : Iterable(String)) : DataFrame

Creates a grouped data-frame given a list of grouping attributes. Most data operations are done on groups defined by variables. group_by() takes the receiver data-frame and converts it into a grouped data-frame where operations are performed "by group". ungroup() removes grouping.

Most verbs like add_column(), summarize(), etc. will be executed per group if a grouping is present.

Source
group_by(*by : String) : DataFrame

Creates a grouped data-frame given a list of grouping attributes. Most data operations are done on groups defined by variables. group_by() takes the receiver data-frame and converts it into a grouped data-frame where operations are performed "by group". ungroup() removes grouping.

Most verbs like add_column(), summarize(), etc. will be executed per group if a grouping is present.

Source
group_by

Creates a grouped data-frame from a column selector function. See select() for details about column selection.

Most data operations are done on groups defined by variables. group_by() takes the receiver data-frame and converts it into a grouped data-frame where operations are performed "by group". ungroup() removes grouping.

Source
group_by_expr(table_expression : TableExpression | Nil = nil) : DataFrame
Source
group_by_expr(exprs : Iterable(TableExpression), table_expression : TableExpression | Nil = nil) : DataFrame
Source
group_by_expr(*exprs : TableExpression, table_expression : TableExpression | Nil = nil) : DataFrame

Creates a grouped data-frame from one or more table expressions. See add_column() for details about table expressions.

Most data operations are done on groups defined by variables. group_by() takes the receiver data-frame and converts it into a grouped data-frame where operations are performed "by group". ungroup() removes grouping.

Source
grouped_by

Returns a data-frame of distinct grouping variable tuples for a grouped data-frame. An empty data-frame for ungrouped data

Source
groups

Returns the groups of a grouped data frame or just a reference to self

Source
head(rows = 5)

return the top rows from dataframe. default to 5

Source
inner_join(right : DataFrame, by : String, suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
inner_join(right : DataFrame, by : Iterable(String) = default_by(self, right), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
inner_join(right : DataFrame, by : Iterable(Tuple(String, String)), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
left_join(right : DataFrame, by : String, suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
left_join(right : DataFrame, by : Iterable(String) = default_by(self, right), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
move_left(*col_names : String) : DataFrame

Push some columns to the left end of a data-frame

Source
move_right(*col_names : String) : DataFrame

Push some columns to the right end of a data-frame

Source
names

Ordered list of column names of this data-frame

Source
nest(col_select : ColumnSelector = ColumnSelector.new(&.except(grouped_by().names)), column_name : String = DEF_NEST_COLUMN_NAME) : DataFrame

Nest repeated values in a list-variable.

There are many possible ways one could choose to nest colSelect inside a data frame. nest() creates a list of data frames containing all the nested variables: this seems to be the most useful form in practice.

Usage

nest(data, ..., column_name = "data")

col_select - A selection of col_select. If not provided, all except the grouping variables are selected. column_name - The name of the new column, as a string or symbol. also see https://github.com/tidyverse/tidyr/blob/master/R/nest.R

Source
nlargest(n : Int32, *columns : String) : DataFrame

Get the n largest rows by column value

df.nlargest(10, "sales")        # Top 10 by sales
df.nlargest(5, "score", "name") # Top 5 by score, ties broken by name
Source
nsmallest(n : Int32, *columns : String) : DataFrame

Get the n smallest rows by column value

df.nsmallest(10, "price")      # Bottom 10 by price
df.nsmallest(5, "age", "name") # Bottom 5 by age, ties broken by name
Source
num_col

Number of columns in this dataframe

Source
num_row

Number of rows in this dataframe

Source
outer_join(right : DataFrame, by : String, suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
outer_join(right : DataFrame, by : Iterable(String) = default_by(self, right), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
pivot_table(index : String | Array(String), columns : String, values : String, aggfunc : String = "mean") : DataFrame

Pivot table - reshape data by aggregating values index: Column(s) to use as row index columns: Column to pivot into new columns values: Column containing values to aggregate aggfunc: Aggregation function ("sum", "mean", "count", "min", "max")

df.pivot_table("region", "product", "sales", "sum")
Source
reject(columns : Iterable(String)) : DataFrame
Source
reject(col_type : DataCol.class) : DataFrame

reject column by column type

Source
reject(*columns : String) : DataFrame

reject selected columns

Source
reject
Source
reject(*col_sels : ColumnSelector) : DataFrame

remove selected columns

Source
reject?

Select or reject columns by predicate

Source
rename(cols : Array(RenamePair)) : DataFrame

Rename one or several columns. Positions should be preserved.

Source
rename(rules : Array(RenameRule)) : DataFrame
Source
rename(*cols : RenamePair) : DataFrame

Rename one or several columns. Positions should be preserved.

Source
rename(*rules : RenameRule) : DataFrame
Source
right_join(right : DataFrame, by : String, suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
right_join(right : DataFrame, by : Iterable(String) = default_by(self, right), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
row(index : Int32) : DataFrameRow

Returns a row by index

Source
row_number

Returns array of row numbers starting from 1

Source
rows

Returns an Iterator over all rows. Per row data is represented as DataFrameRow

Source
rowwise

Creates a grouped data-frame where each group consists of exactly one line.

Source
sample(n : Int32, seed : Int32 | Nil = nil) : DataFrame

Random sample of rows

Source
sample(frac : Float64, seed : Int32 | Nil = nil) : DataFrame

Random sample by fraction

Source
sample_frac(fraction : Float64, replace = false) : DataFrame

Select random rows from a table. If receiver is grouped, sampling is done per group. fraction - Fraction of rows to sample replace - Sample with or without replacement

Source
sample_n(n : Int32, replace = false) : DataFrame

Select random rows from a table. If receiver is grouped, sampling is done per group. n - Number of rows to sample replace - Sample with or without replacement

Source
schema(max_digits = 3, max_width = PRINT_MAX_WIDTH, output = STDOUT)

Prints the schema (that is column names, types, and the first few values per column) of a dataframe to output (defaults to STDOUT).

Source
select(columns : Iterable(String)) : DataFrame

Create a new data frame with only selected columns

Source
select(col_type : DataCol.class) : DataFrame

Select column by column type

Source
select(which : Array(Bool | Nil)) : DataFrame
Source
select(*columns : String) : DataFrame
Source
select

Keeps only the variables that match any of the given expression

Source
select(*col_sels : ColumnSelector) : DataFrame
Source
select?

Select or reject columns by predicate

Source
semi_join(right : DataFrame, by : String) : DataFrame

Special case of inner join against distinct right side

Source
semi_join(right : DataFrame, by : Iterable(Tuple(String, String))) : DataFrame
Source
semi_join(right : DataFrame, by : Iterable(String) = default_by(self, right), suffices : Tuple(String, String) = {".x", ".y"}) : DataFrame
Source
separate(column : String, into : Array(String), sep : Regex | String = /[^\w]/, remove : Bool = true, convert : Bool = false) : DataFrame

Given either regular expression or a vector of character positions, separate() turns a single character column into multiple columns.

column - Bare column name. into - Names of new variables to create as character vector. sep - Separator between columns. If String, is interpreted as a regular expression. The default value is a regular expression that matches any sequence of non-alphanumeric values. remove - If true, remove input column from output data frame. convert - If set, attempt to do a type conversion will be run on all new columns. This is useful if the value column was a mix of variables that was coerced to a string.

Source
set_names(new_names : Array(String)) : DataFrame

Replace current column names with new ones. The number of provided names must match the number of columns.

Source
set_names(*new_name : String) : DataFrame

Replace current column names with new ones. The number of provided names must match the number of columns.

Source
shuffle(seed : Int32 | Nil = nil) : DataFrame

Shuffle rows randomly

Source
slice(slices : Range)

Select rows by position while taking into account grouping in a data-frame.

Source
slice(*slices : Int32)

Select rows by position while taking into account grouping in a data-frame.

Source
sort_by(by : Iterable(String)) : DataFrame

Resorts the receiver in ascending order (small values to go top of table). The first argument defines the primary attribute to sort by. Additional ones are used to resolve ties.

Missing values will come last in the sorted table.

Source
sort_by(exp : Iterable(SortExpression))
Source
sort_by
Source
sort_by(*by : String) : DataFrame
Source
sort_by
Source
sort_desc_by(*by : String) : DataFrame

Resorts the receiver in descending order (small values to go bottom of table). The first argument defines the primary attribute to sort by. Additional ones are used to resolve ties.

Source
spread(key : String, value : String, fill = nil, convert = false) : DataFrame

spread a key-value pair across multiple columns.

key The bare (unquoted) name of the column whose values will be used as column headings. value The bare (unquoted) name of the column whose values will populate the cells. fill If set, missing values will be replaced with this value - NOT IMPLEMENTED convert If set, attempt to do a type conversion will be run on all new columns. This is useful if the value column was a mix of variables that was coerced to a string.

Source
summarize(name : String, block : TableExpression) : DataFrame
Source
summarize(sum_rules : Array(ColumnFormula)) : DataFrame

Creates a summary of a table or a group. The provided expression is expected to evaluate to a scalar value and not into a column. summarize() is typically used on grouped data created by group_by(). The output will have one row for each group.

Source
summarize(*sum_rules : ColumnFormula) : DataFrame
Source
summarize(name : String, &block : TableExpression) : DataFrame
Source
summarize_at
Source
summarize_at(col_sel : ColumnSelector, aggfuns : Array(AggFunc)) : DataFrame
Source
summarize_at(col_sel : ColumnSelector, op : SummarizeFunc | Nil = nil) : DataFrame
Source
summarize_at(col_sel : ColumnSelector, *aggfuns : AggFunc) : DataFrame
Source
tail(rows = 5)
Source
take(rows = 5)
Source
take_last(rows : Int32)
Source
to_h

Expose a view on the data as Hash from column names to nullable arrays.

Source
to_json(pretty : Bool = false) : String

Convert DataFrame to JSON string

Source
to_s(io : Nil)
Source
to_s
Source
to_string(title = "A DataFrame", col_names = true, max_rows = PRINT_MAX_ROWS, max_width = PRINT_MAX_WIDTH, max_digits = PRINT_MAX_DIGITS, row_numbers = PRINT_ROW_NUMBERS)

Converts dataframe to its string representation. This is being invoked via print and to_s

Source
transmute(*formula : ColumnFormula)

Create a new dataframe based on a list of column-formulas which are evaluated in the context of the this instance.

Source
ungroup

Removes the grouping (if present from a data frame)

Source
unite(col_name : String, which : Array(String), sep : String = "_", remove : Bool = true) : DataFrame

Convenience function to paste together multiple columns into one.

colName - Name of the column to add which - Names of columns which should be concatenated together sep - Separator to use between values. remove - If true, remove input columns from output data frame.

see separate

Source
unite(col_name : String, *which : ColumnSelector, sep : String = "_", remove : Bool = true) : DataFrame
Source
unnest(column_name : String) : DataFrame

If you have a list-column, this makes each element of the list its own row. It unfolds data vertically. unnest() can handle list-columns that can atomic vectors, lists, or data frames (but not a mixture of the different types).

Source
value_counts(column : String) : DataFrame

Value counts for a column - returns DataFrame with value and count

Source
write_csv(filename : String, separator : Char = ',', quote_char : Char = '"') : Nil

Save the current dataframe to separator delimited file.

Source
write_csv(io : IO, separator : Char = ',', quote_char : Char = '"') : Nil
Source
write_json(filename : String, pretty : Bool = false) : Nil

Write DataFrame to JSON file (array of objects format)

df.write_json("output.json")
df.write_json("output.json", pretty: true)
Source
write_json(io : IO, pretty : Bool = false) : Nil

Write DataFrame to IO as JSON (array of objects format)

Source