Documentation
¶
Index ¶
- type Column
- func (c *Column) Count() int
- func (c *Column) Map(f func(any) any) *Column
- func (c *Column) Max() (float64, bool)
- func (c *Column) Mean() (float64, bool)
- func (c *Column) Median() (float64, bool)
- func (c *Column) Min() (float64, bool)
- func (c *Column) Missing() int
- func (c *Column) Name() string
- func (c *Column) Normalize() (*Column, error)
- func (c *Column) Q1() (float64, bool)
- func (c *Column) Q3() (float64, bool)
- func (c *Column) Quantile(q float64) (float64, bool)
- func (c *Column) Standardize() (*Column, error)
- func (c *Column) Std() (float64, bool)
- func (c *Column) Sum() (float64, bool)
- func (c *Column) Values() []any
- type Table
- func (t *Table) AddColumn(name string, values []any) (*Table, error)
- func (t *Table) AddColumns(args map[string][]any) (*Table, error)
- func (t *Table) Categorize() *Table
- func (t *Table) Clone() *Table
- func (t *Table) Col(name string) (*Column, error)
- func (t *Table) Columns() []string
- func (t *Table) Display()
- func (t *Table) DisplayTranspose()
- func (t *Table) Drop(cols ...string) (*Table, error)
- func (t *Table) First(n ...int) *Table
- func (t *Table) GetColumnIndex(columnName string) (int, error)
- func (t *Table) HasColumn(columnName string) bool
- func (t *Table) Last(n ...int) *Table
- func (t *Table) Len() int
- func (t *Table) MapCol(name string, f func(any) any) (*Table, error)
- func (t *Table) MustCol(name string) *Column
- func (t *Table) MustGetColumnIndex(columnName string) int
- func (t *Table) MustNumericSlice(columnIndex int) []float64
- func (t *Table) MustSelectRows(indexes []int) *Table
- func (t *Table) NumericMatrix() ([][]float64, error)
- func (t *Table) NumericSlice(columnIndex int) ([]float64, error)
- func (t *Table) Overview()
- func (t *Table) ReplaceColumn(name string, values []any) error
- func (t *Table) Sample(n ...int) *Table
- func (t *Table) Select(cols ...string) (*Table, error)
- func (t *Table) SelectRows(indexes []int) (*Table, error)
- func (t *Table) Stats()
- func (t *Table) Where(f func(row map[string]any) bool) (*Table, error)
- func (t *Table) WriteCSV(filename string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column represents a single column in a table.
A column holds its name, underlying data, and metadata inferred from its values (such as whether it should be treated as categorical).
func (*Column) Count ¶
Count returns the number of non-missing values in the column.
A value is considered missing if it is nil or an empty string.
func (*Column) Map ¶
Map applies the provided function `f` to each value in the Column and returns a new Column containing the results. The original Column remains unchanged.
Parameters:
- f: a function that takes an `any` value and returns a transformed `any` value.
Returns:
- *Column: a new Column with the mapped values.
func (*Column) Max ¶
Max returns the maximum numeric value in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Mean ¶
Mean returns the arithmetic mean of all numeric values in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Median ¶
Median returns the median (0.5 quantile) of the numeric values in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Min ¶
Min returns the minimum numeric value in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Missing ¶
Missing returns the number of missing values in the column.
A value is considered missing if it is nil or an empty string.
func (*Column) Normalize ¶
Normalize performs Min-Max normalization on the column.
The values are rescaled to the range [0, 1] using the formula:
(x - min) / (max - min)
Only numeric columns are supported. If the column contains non-numeric values or has zero variance (min == max), an error is returned.
Normalize does not mutate the original column. A new Column instance with normalized values is returned.
func (*Column) Q1 ¶
Q1 returns the first quartile (0.25 quantile) of the numeric values in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Q3 ¶
Q3 returns the third quartile (0.75 quantile) of the numeric values in the column.
Non-numeric values are ignored. The second return value is false if the column contains no numeric values.
func (*Column) Quantile ¶
Quantile returns the q-th quantile of the numeric values in the column.
The parameter q must be in the range [0, 1]. Non-numeric values are ignored. Linear interpolation is used between adjacent values. The second return value is false if the column contains no numeric values or if q is out of range.
func (*Column) Standardize ¶
Standardize performs Z-score standardization on the column.
The values are transformed to have zero mean and unit variance using the formula:
(x - mean) / std
Only numeric columns are supported. If the column contains non-numeric values or has zero standard deviation, an error is returned.
Standardize does not mutate the original column. A new Column instance with standardized values is returned.
func (*Column) Std ¶
Std returns the sample standard deviation of numeric values in the column.
Non-numeric values are ignored. The second return value is false if fewer than two numeric values are present.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table represents a simple in-memory table structure. It contains the column names, the underlying data per column, and the number of rows.
func EmptyTableFrom ¶ added in v0.2.0
EmptyTableFrom creates and returns a new empty Table with the same column headers as the given Table. The returned Table has zero rows, but preserves the column names. This is useful for operations like SelectRows([]int{}) or filtering that result in no rows.
Example:
t2 := EmptyTableFrom(t) fmt.Println(t2.Length()) // 0 fmt.Println(t2.Columns()) // same as t.Columns()
func New ¶
New constructs a new Table from a map of column names to slices of values. All columns must have the same length; otherwise, an error is returned.
Parameters:
- data: map[string][]any, where each key is a column name and the value is a slice of values.
- columnsOrder (optional): variadic slice specifying the desired order of columns in the Table. If provided and not nil, the Table will use this order. If not provided, the order will follow the order of iteration over the map (which is non-deterministic in Go). Only the first slice is used if multiple slices are provided.
Returns:
- *Table: the constructed Table instance
- error: error if the data is empty, if a column in columnsOrder is missing, or if column lengths are inconsistent.
func NewEmptyTable ¶ added in v0.2.0
func NewEmptyTable() *Table
NewEmptyTable creates and returns a completely empty Table. The table has no columns and no rows. This can be used as a placeholder or initial value when no column information is available.
Example:
t := NewEmptyTable() fmt.Println(t.Length()) // 0 fmt.Println(t.Columns()) // []
func (*Table) AddColumn ¶
AddColumn returns a new Table with a single column appended.
This is a convenience wrapper around AddColumns for adding one column. The original table is not modified.
func (*Table) AddColumns ¶
AddColumns returns a new Table with one or more columns appended.
Each entry (key and value) in args represents a new column name and its values. All provided columns must:
- have non-empty names
- not already exist in the table
- have the same number of rows as the table
The original table is not modified.
func (*Table) Categorize ¶
Categorize returns a new Table where each categorical column produces an additional column with encoded integer values.
For every categorical column, a new column named "<column>_categorized" is appended. Each unique value in the original column is mapped to a zero-based integer, preserving row order. Non-categorical columns are copied as-is.
The original Table is not modified.
func (*Table) Clone ¶
Clone creates a deep copy of the table.
The cloned table has its own copy of column metadata and underlying data slices, so modifications to the returned table do not affect the original table, and vice versa.
Clone preserves:
- column order
- column names
- row count
This method is intended for non-mutating operations (e.g. normalization, scaling, feature transformation) where a transformed table should be produced without altering the original data.
func (*Table) Col ¶
Col returns a column by name.
The returned column contains a copy of the underlying data, so changes to the column do not mutate the original table. An error is returned if the column does not exist.
func (*Table) Columns ¶
Columns returns a copy of the column names in their current order. Modifying the returned slice will not affect the Table.
func (*Table) Display ¶
func (t *Table) Display()
Display prints the table to the standard output.
It renders all rows of the table using the current column order. If the table is nil, the string "nil" is printed instead.
func (*Table) DisplayTranspose ¶ added in v0.3.0
func (t *Table) DisplayTranspose()
func (*Table) Drop ¶
Drop returns a new table with the specified columns removed, while preserving the order of the remaining columns.
If no columns are provided, a shallow copy of the table is returned. An error is returned if any specified column does not exist. The original table is not modified.
func (*Table) First ¶
First returns a new table containing the first n rows of the original table.
If n is not provided, First uses the default number of rows that is 5. If n exceeds the table length, all rows are returned.
The original table is not modified.
func (*Table) GetColumnIndex ¶
GetColumnIndex returns the index of the column with the given name.
func (*Table) Last ¶
Last returns a new table containing the last n rows of the original table.
If n is not provided, Last uses the default number of rows that is 5. If n exceeds the table length, all rows are returned.
The original table is not modified.
func (*Table) MapCol ¶
MapCol applies the provided function `f` to all values in the specified column of the Table, returning a new Table with the updated column. All other columns remain unchanged. The original Table is not modified.
Parameters:
- name: the name of the column to transform
- f: a function that takes an `any` value and returns a transformed `any` value
Returns:
- *Table: a new Table with the transformed column
- error: if the specified column does not exist
func (*Table) MustCol ¶
MustCol returns a column by name without returning an error.
If the column does not exist, an empty column with the given name is returned. This method is intended for fluent or chainable usage where error handling is intentionally omitted.
func (*Table) MustGetColumnIndex ¶
MustGetColumnIndex returns the column index or panics if not found.
func (*Table) MustNumericSlice ¶ added in v0.1.1
MustNumericSlice returns a slice of float64 values for the column at the specified index.
This method panics if:
- columnIndex is out of range
- any value in the column cannot be converted to float64
MustNumericSlice assumes that the Table was constructed correctly:
- all columns exist in the table's data map
- all columns have the same length as the table
Use this method only when you are certain that the column contains numeric values. It is intended for internal or performance-sensitive code where error handling via panic is acceptable.
func (*Table) MustSelectRows ¶ added in v0.2.0
MustSelectRows returns a new Table containing only the rows specified by the given indices. It panics if any index is out of bounds. The original Table is not modified.
Example:
t2 := t.MustSelectRows([]int{0, 2, 5}) // panics if index invalid
func (*Table) NumericMatrix ¶
NumericMatrix returns all columns as [][]float64 (row-major).
func (*Table) NumericSlice ¶
NumericSlice returns a numeric column as []float64 by column index.
func (*Table) Overview ¶
func (t *Table) Overview()
Overview prints a summary of the table to the standard output.
It displays the total number of rows and a metadata table describing each column, including its name and inferred data type. If the table is nil, the string "nil" is printed instead.
func (*Table) ReplaceColumn ¶
ReplaceColumn replaces the data of an existing column with the provided values.
The column must already exist in the table. The length of values must match the table length.
This method does not modify the column order.
func (*Table) Sample ¶
Sample returns a new table containing n randomly sampled rows from the original table.
If n is not provided, Sample uses the default number of rows that is 5. Sampling is performed without replacement. If n exceeds the table length, all rows are returned in randomized order.
The original table is not modified.
func (*Table) Select ¶
Select returns a new table containing only the specified columns, preserving their order as provided in the arguments.
An error is returned if no columns are specified or if any column does not exist in the table. The original table is not modified.
func (*Table) SelectRows ¶ added in v0.2.0
SelectRows returns a new Table containing only the rows specified by the given indices. If any index is out of bounds, an error is returned. The original Table is not modified.
Example:
t2, err := t.SelectRows([]int{0, 2, 5})
if err != nil {
// handle error
}
func (*Table) Stats ¶
func (t *Table) Stats()
Stats computes and displays descriptive statistics for all numeric columns in the table.
The statistics include count, missing values, mean, standard deviation, minimum, quartiles (Q1, median, Q3), and maximum. Only columns containing numeric data are included in the output.
The result is rendered as a table and printed directly to stdout. This method does not return a value and does not modify the original table. If the table is nil or empty, a message is printed instead.
func (*Table) Where ¶
Where filters rows of the Table using a predicate function and returns a new Table containing only rows for which the predicate returns true.
The predicate function receives a map representing a single row, where keys are column names and values are the corresponding cell values. All columns are preserved in the resulting Table.
Returns an error only if Table construction fails.
func (*Table) WriteCSV ¶
WriteCSV writes the Table to a CSV file specified by filename.
The function creates a temporary file in the same directory as filename and writes all table data to it. If writing succeeds, the temporary file is renamed to the target filename. If any error occurs during writing or flushing, the temporary file is removed to avoid leaving a partial/corrupt file.
If a file with the specified name already exists, it may be overwritten.
The CSV output format:
- The first row contains the column headers in the order defined in the Table.
- Each subsequent row contains the corresponding values of the Table as strings.
- Missing values are written as empty strings.
Error conditions:
- if the table has no data or no columns
- if creating the temporary file fails
- if writing the header or any row fails
- if flushing the writer fails
- if closing or renaming the temporary file fails