class Dataframe

Overview

NOTE The CSV Spec(RFC 4180) does state that leading and trailing whitespaces should not be ignored, some implementations of CSV do still use them. For this reason, Dataframe will ignore leading and trailing whitespace in unquoted cells.

Defined in:

builder/csv_builder.cr
column.cr
common.cr
dataframe.cr
parser/csv_lexer.cr
parser/csv_parser.cr
row.cr

Constant Summary

VERSION = "0.1.0"

Constructors

Instance Method Summary

Constructor Detail

def self.from_csv(string_or_io : String | IO, headers : Bool = true, separator : Char = CSVLexer::DEFAULT_SEPARATOR, quote_char : Char = CSVLexer::DEFAULT_QUOTE_CHAR) : Dataframe #

def self.new(headers : Array(String), rows : Array(Array(Type))) #

Creates a new Dataframe instance with the given headers and rows.

Raises an InvalidDataframe error if the headers and each row don't all have the same length.


def self.new(column_names : Array(String)) #

Creates a new Dataframe instance with the specified headers, and columns of type String, but with no data.


def self.new(columns) #

Creates a new Dataframe instance with column names and types defined by columns, but with no data.


def self.new #

Creates an empty Dataframe instance, with no columns or rows.


Instance Method Detail

def <<(row : Array(Type)) #

Append. Alias for #add_row.


def <<(row : Row) #

Append. Alias for #add_row.


def ==(other : Dataframe) : Bool #

def [](header : String) : Dataframe::Column #

def []=(header : String, new_column : Column) : self #

def add_column(header : String, type : ColumnType = String) #

Adds a new empty column to self.


def add_column(header : String, data : Array(Type)) #

Adds a new column to self, with content specified by data.

The type is determined by the content of data.

NOTE: If data doesn't have any non-null values, a runtime error will occur.


def add_row(row : Array(Type)) #

Append a new row to the bottom of self.


def add_row(row : Row) #

Append a new row to the bottom of self.


def columns : Hash(String, Column(String) | Column(Int32) | Column(Float64) | Column(Bool)) #

Returns the columns of self as a Hash with the headers as keys, and Columns as values.

NOTE: Because this method returns a union of all possible types, it's necessary to cast the column as the proper type before running any type specific methods.

column = dataframe.columns["Age"].as(Dataframe::Column(Int32))

column.map! do |e|
  e.nil? ? e : e + 1
end

def data : Array(Array(Bool | Float64 | Int32 | String | Nil)) #

def each(& : Array(Type) -> ) : Nil #

Iterates over the rows of self.


def each_row(& : Row -> ) : Nil #

Iterates over the rows of self, returning each row as an instance of Row.


def full_join(other : Dataframe, on : Array(String)) : Dataframe #

Returns a new Dataframe that is the result of a full join of the receiver and other, using the headers in on to match rows.


def headers : Array(String) #

Returns the column names as an Array.


def inner_join(other : Dataframe, on : Array(String)) : Dataframe #

Returns a new Dataframe that is the result of an inner join of the receiver and other, using the headers in on to match rows.


def left_join(other : Dataframe, on : Array(String)) : Dataframe #

Returns a new Dataframe that is the result of a left outer join of the receiver and other, using the headers in on to match rows.


def order_columns(new_headers : Array(String)) : Dataframe #

Returns a new Dataframe with columns ordered by new_headers.

NOTE: Any column with names omitted from new_headers will not be included in the new Dataframe.

See also: Dataframe#select.


def order_columns!(new_headers : Array(String)) : self #

Modifies self by rearranging columns in order specified by new_headers.

NOTE: Any column with names omitted from new_headers will be removed from self.

See also: Dataframe#select!.


def reject(& : Row -> ) : Dataframe #

Returns a Dataframe with all the elements in the collection for which the passed block is falsey.


def reject!(& : Row -> ) : self #

Modifies self, deleting the rows in the collection for which the passed block is truthy. Returns self.

See also: Dataframe#reject.


def reject_columns(headers : Array(String)) : Dataframe #

Returns a new Dataframe without the given columns.


def reject_columns!(headers : Array(String)) : self #

Removes a list of columns.


def rename_column(old_header, new_header) #

Changes the header of the specified column to a new value.

Makes no changes if old_header isn't a header.


def right_join(other : Dataframe, on : Array(String)) : Dataframe #

Returns a new Dataframe that is the result of a right outer join of the receiver and other, using the headers in on to match rows.


def row_count #

Returns the number of rows in the Dataframe.


def rows : Array(Row) #

Returns the data of the Dataframe as an array of Row.


def select(& : Row -> ) : Dataframe #

Returns a new Dataframe with only rows for which the passed block is truthy.


def select!(& : Row -> ) : self #

Returns a new Dataframe with only rows for which the passed block is truthy.


def select_columns(headers : Array(String)) : Dataframe #

Returns a new Dataframe with the given columns.


def select_columns!(headers : Array(String)) : self #

Removes every column except the given ones.


def shape : Tuple(Int32, Int32) #

Returns a Tuple of the dataframe's dimensions in the form of { rows, columns }


def sort_by(& : Row -> ) : Dataframe #

def sort_by(column : String, desc = false) : Dataframe #

def sort_by!(& : Row -> ) : self #

def sort_by!(column : String, desc = false) : self #

def to_csv(separator : Char = CSV::DEFAULT_SEPARATOR, quote_char : Char = CSV::DEFAULT_QUOTE_CHAR) : String #