datatable

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

README

datatable

Circle CI Go Report Card GoDoc GitHub stars GitHub license

datasweet-logo

datatable is a Go package to manipulate tabular data, like an excel spreadsheet. datatable is inspired by the pandas python package and the data.frame R structure. Although it's production ready, be aware that we're still working on API improvements

Installation

go get github.com/datasweet/datatable

Features

  • Create custom Series (ie custom columns). Currently available, serie.Int, serie.String, serie.Time, serie.Float64.
  • Apply formulas
  • Selects (head, tail, subset)
  • Sorting
  • InnerJoin, LeftJoin, RightJoin, OuterJoin, Concats
  • Export to map, slice
Creating a DataTable

import (
	"github.com/datasweet/datatable"
	"github.com/datasweet/datatable/serie"
)

func main() {
  tb := datatable.New("test")
	tb.AddColumn("champ", serie.String("Malzahar", "Xerath", "Teemo"))
	tb.AddExprColumn("champion", serie.String(), "upper(`champ`)")
	tb.AddColumn("win", serie.Int(10, 20, 666))
	tb.AddColumn("loose", serie.Int(6, 5, 666))
	tb.AddExprColumn("winRate", serie.String(), "(`win` * 100 / (`win` + `loose`)) ~ \" %\"")
	tb.AddExprColumn("sum", serie.Float64(), "sum(`win`)")
  tb.AddExprColumn("ok", serie.Bool(), "true")

  fmt.Println(tb)
}

/*
CHAMP <STRING>	CHAMPION <STRING>	WIN <INT>	LOOSE <INT>	WINRATE <STRING>	SUM <FLOAT64>	OK <BOOL> 
Malzahar      	MALZAHAR         	10       	6          	62.5 %          	696          	true     	
Xerath        	XERATH           	20       	5          	80 %            	696          	true     	
Teemo         	TEEMO            	666      	666        	50 %            	696          	true     	
*/
Creating a custom serie

To create a custom serie you must provide:

  • a caster function, to cast a generic value to your serie value. The signature must be func(i interface{}) T
  • a comparator, to compare your serie value. The signature must be func(a, b T) int

Example with a NullInt

// IntN is an alis to create the custom Serie to manage IntN
func IntN(v ...interface{}) Serie {
	s, _ := New(NullInt{}, asNullInt, compareNullInt)
	if len(v) > 0 {
		s.Append(v...)
	}
	return s
}

type NullInt struct {
	Int   int
	Valid bool
}

// Interface() to render the current struct as a value.
// If not provided, the serie.All() or serie.Get() wills returns the embedded value
// IE: NullInt{}
func (i NullInt) Interface() interface{} {
	if i.Valid {
		return i.Int
	}
	return nil
}

// asNullInt is our caster function
func asNullInt(i interface{}) NullInt {
	var ni NullInt
	if i == nil {
		return ni
	}

	if v, ok := i.(NullInt); ok {
		return v
	}

	if v, err := cast.ToIntE(i); err == nil {
		ni.Int = v
		ni.Valid = true
	}
	return ni
}

// compareNullInt is our comparator function
// used to sort
func compareNullInt(a, b NullInt) int {
	if !b.Valid {
		if !a.Valid {
			return Eq
		}
		return Gt
	}
	if !a.Valid {
		return Lt
  }
  if a.Int == b.Int {
		return Eq
	}
	if a.Int < b.Int {
		return Lt
	}
	return Gt
}

Who are we ?

We are Datasweet, a french startup providing full service (big) data solutions.

Questions ? problems ? suggestions ?

If you find a bug or want to request a feature, please create a GitHub Issue.

License

This software is licensed under the Apache License, version 2 ("ALv2"), quoted below.

Copyright 2017-2020 Datasweet <http://www.datasweet.fr>

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type By

type By struct {
	Column string
	Desc   bool
	// contains filtered or unexported fields
}

By defines a sort to be applied

type Column

type Column interface {
	Name() string
	Type() reflect.Type
	IsVisible() bool
	IsComputed() bool
}

Column describes a column in our datatable

type DataTable

type DataTable struct {
	// contains filtered or unexported fields
}

DataTable is our main struct

func Concat

func Concat(tables []*DataTable) (*DataTable, error)

Concat datatables

func InnerJoin

func InnerJoin(tables []*DataTable, on []JoinOn) (*DataTable, error)

InnerJoin selects records that have matching values in both tables. tables[0] is used as reference datatable.

func LeftJoin

func LeftJoin(tables []*DataTable, on []JoinOn) (*DataTable, error)

LeftJoin the tables. tables[0] is used as reference datatable.

func New

func New(name string) *DataTable

New creates a new datatable

func OuterJoin

func OuterJoin(tables []*DataTable, on []JoinOn) (*DataTable, error)

OuterJoin the tables. tables[0] is used as reference datatable.

func RightJoin

func RightJoin(tables []*DataTable, on []JoinOn) (*DataTable, error)

RightJoin the tables. tables[0] is used as reference datatable.

func (*DataTable) AddBoolColumn

func (t *DataTable) AddBoolColumn(name string, v ...interface{}) error

AddBoolColumn to add a column of nullable bool

func (*DataTable) AddBoolExprColumn

func (t *DataTable) AddBoolExprColumn(name string, expr string) error

AddBoolExprColumn to add a calculated column of nullable bool

func (*DataTable) AddColumn

func (t *DataTable) AddColumn(name string, sr serie.Serie) error

AddColumn to datatable with a serie of T

func (*DataTable) AddExprColumn

func (t *DataTable) AddExprColumn(name string, sr serie.Serie, formulae string) error

AddExprColumn to add a calculated column with a serie of T

func (*DataTable) AddFloat32Column

func (t *DataTable) AddFloat32Column(name string, v ...interface{}) error

AddFloat32Column to add a column of nullable float32

func (*DataTable) AddFloat32ExprColumn

func (t *DataTable) AddFloat32ExprColumn(name string, expr string) error

AddFloat32ExprColumn to add a calculated column of nullable bool

func (*DataTable) AddFloat64Column

func (t *DataTable) AddFloat64Column(name string, v ...interface{}) error

AddFloat64Column to add a column of nullable float64

func (*DataTable) AddFloat64ExprColumn

func (t *DataTable) AddFloat64ExprColumn(name string, expr string) error

AddFloat64ExprColumn to add a calculated column of nullable bool

func (*DataTable) AddInt32Column

func (t *DataTable) AddInt32Column(name string, v ...interface{}) error

AddInt32Column to add a column of nullable int32

func (*DataTable) AddInt32ExprColumn

func (t *DataTable) AddInt32ExprColumn(name string, expr string) error

AddInt32ExprColumn to add a calculated column of nullable int32

func (*DataTable) AddInt64Column

func (t *DataTable) AddInt64Column(name string, v ...interface{}) error

AddInt64Column to add a column of nullable int64

func (*DataTable) AddInt64ExprColumn

func (t *DataTable) AddInt64ExprColumn(name string, expr string) error

AddInt64ExprColumn to add a calculated column of nullable int32

func (*DataTable) AddIntColumn

func (t *DataTable) AddIntColumn(name string, v ...interface{}) error

AddIntColumn to add a column of nullable int

func (*DataTable) AddIntExprColumn

func (t *DataTable) AddIntExprColumn(name string, expr string) error

AddIntExprColumn to add a calculated column of nullable int

func (*DataTable) AddStringColumn

func (t *DataTable) AddStringColumn(name string, v ...interface{}) error

AddStringColumn to add a column of nullable string

func (*DataTable) AddStringExprColumn

func (t *DataTable) AddStringExprColumn(name string, expr string) error

AddStringExprColumn to add a calculated column of nullable bool

func (*DataTable) AddTimeColumn

func (t *DataTable) AddTimeColumn(name string, v ...interface{}) error

AddTimeColumn to add a column of nullable time

func (*DataTable) AddTimeExprColumn

func (t *DataTable) AddTimeExprColumn(name string, expr string) error

AddTimeExprColumn to add a calculated column of nullable bool

func (*DataTable) Append

func (t *DataTable) Append(row ...Row)

Append rows to the table

func (*DataTable) AppendRow

func (t *DataTable) AppendRow(v ...interface{}) error

AppendRow creates a new row and append cells to this row

func (*DataTable) Column

func (t *DataTable) Column(name string) Column

Column gets the column with name returns nil if not found

func (*DataTable) ColumnIndex

func (t *DataTable) ColumnIndex(name string) int

ColumnIndex gets the index of the column with name returns -1 if not found

func (*DataTable) Columns

func (t *DataTable) Columns() []string

Columns returns the visible column names in datatable

func (*DataTable) Concat

func (left *DataTable) Concat(table ...*DataTable) (*DataTable, error)

Concat datatables

func (*DataTable) Copy

func (t *DataTable) Copy() *DataTable

Copy the datatable

func (*DataTable) EmptyCopy

func (t *DataTable) EmptyCopy() *DataTable

EmptyCopy copies the structure of datatable (no values)

func (*DataTable) Grow

func (t *DataTable) Grow(size int)

Grow the table by size

func (*DataTable) Head

func (t *DataTable) Head(size int) *DataTable

Head selects {size} first rows

func (*DataTable) HiddenColumns

func (t *DataTable) HiddenColumns() []string

HiddenColumns returns the hidden column names in datatable

func (*DataTable) HideColumn

func (t *DataTable) HideColumn(name string)

HideColumn hides a column a hidden column will not be exported

func (*DataTable) InnerJoin

func (left *DataTable) InnerJoin(right *DataTable, on []JoinOn) (*DataTable, error)

InnerJoin selects records that have matching values in both tables. left datatable is used as reference datatable. <!> InnerJoin transforms an expr column to a raw column

func (*DataTable) LeftJoin

func (left *DataTable) LeftJoin(right *DataTable, on []JoinOn) (*DataTable, error)

LeftJoin returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match. <!> LeftJoin transforms an expr column to a raw column

func (*DataTable) Name

func (t *DataTable) Name() string

Name returns the datatable's name

func (*DataTable) NewRow

func (t *DataTable) NewRow() Row

NewRow create a new row

func (*DataTable) NumCols

func (t *DataTable) NumCols() int

NumCols returns the number of visible columns in datatable

func (*DataTable) NumRows

func (t *DataTable) NumRows() int

NumRows returns the number of rows in datatable

func (*DataTable) OuterJoin

func (left *DataTable) OuterJoin(right *DataTable, on []JoinOn) (*DataTable, error)

OuterJoin returns all records when there is a match in either left or right table <!> OuterJoin transforms an expr column to a raw column

func (*DataTable) Print

func (t *DataTable) Print(writer io.Writer, opt ...PrintOption)

Print the tables with options

func (*DataTable) Records

func (t *DataTable) Records() [][]string

Records returns the rows in datatable as string Computes all expressions.

func (*DataTable) Rename

func (t *DataTable) Rename(name string)

Rename the datatable

func (*DataTable) RenameColumn

func (t *DataTable) RenameColumn(old, name string) error

RenameColumn to rename a column

func (*DataTable) RightJoin

func (left *DataTable) RightJoin(right *DataTable, on []JoinOn) (*DataTable, error)

RightJoin returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match. <!> RightJoin transforms an expr column to a raw column

func (*DataTable) Row

func (t *DataTable) Row(at int) Row

Row gets the row at index

func (*DataTable) Rows

func (t *DataTable) Rows() []Row

Rows returns the rows in datatable Computes all expressions.

func (*DataTable) ShowColumn

func (t *DataTable) ShowColumn(name string)

ShowColumn shows a column a shown column will be exported

func (*DataTable) Sort

func (t *DataTable) Sort(by ...By) *DataTable

Sort the table

func (*DataTable) String

func (t *DataTable) String() string

func (*DataTable) Subset

func (t *DataTable) Subset(at, size int) *DataTable

Subset selects rows at index with size

func (*DataTable) SwapColumn

func (t *DataTable) SwapColumn(a, b string) error

SwapColumn to swap 2 columns

func (*DataTable) SwapRow

func (t *DataTable) SwapRow(i, j int)

SwapRow in table

func (*DataTable) Tail

func (t *DataTable) Tail(size int) *DataTable

Tail selects {size} last rows

func (*DataTable) ToMap

func (t *DataTable) ToMap() []map[string]interface{}

ToMap to export the datatable to a json-like struct

func (*DataTable) ToSchema

func (t *DataTable) ToSchema() *Schema

ToSchema to export the datatable to a schema struct

func (*DataTable) ToTable

func (t *DataTable) ToTable() [][]interface{}

ToTable to export the datatable to a csv-like struct

func (*DataTable) Update

func (t *DataTable) Update(at int, row Row) error

Update the row at index

type JoinOn

type JoinOn struct {
	Table string
	Field string
}

func On

func On(fields ...string) []JoinOn

On creates a "join on" expression ie, as SQL, SELECT * FROM A INNER JOIN B ON B.id = A.user_id Syntax: "[table].[field]", "field"

func Using

func Using(fields ...string) []JoinOn

Using creates a "join using" expression ie, as SQL, SELECT * FROM A INNER JOIN B USING 'field'

type PrintOption

type PrintOption func(opts *PrintOptions)

func PrintColumnName

func PrintColumnName(v bool) PrintOption

func PrintColumnType

func PrintColumnType(v bool) PrintOption

func PrintMaxRows

func PrintMaxRows(v int) PrintOption

func PrintRowNumber

func PrintRowNumber(v bool) PrintOption

type PrintOptions

type PrintOptions struct {
	ColumnName bool
	ColumnType bool
	RowNumber  bool
	MaxRows    int
}

PrintOptions to control the printer

type Row

type Row map[string]interface{}

Row contains a row relative to columns

func (Row) Get

func (r Row) Get(k string) interface{}

Get cell

func (Row) Hash

func (r Row) Hash() (uint64, bool)

Hash computes the hash code from this datarow can be used to filter the table (distinct rows)

func (Row) Set

func (r Row) Set(k string, v interface{}) Row

Set cell

type Schema

type Schema struct {
	Name    string          `json:"name"`
	Columns []SchemaColumn  `json:"cols"`
	Rows    [][]interface{} `json:"rows"`
}

Schema describes a datatable

type SchemaColumn

type SchemaColumn struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL