dataframe

package
v0.0.0-...-460a440 Latest Latest
Warning

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

Go to latest
Published: May 1, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

* Package dataframe provides a DatFrame structure for handle table data.

Create a DataFrame from struct array

It can create a DataFrame using a struct array. The DataFrame columns will be defined in the golang struct.

Example:

// Go struct to defined the Dataframe struct.
type myDataFrame struct {
	// The DataFrame will create a column, type int, with the name colA
	A int `colName:"colA"`
	// This field is public, but as it has not the colName tag,
	// it will be ignored by DataFrame
	B int
	// The DataFrame will create a column, type float32, with the name float
	C float32 `colName:"float"`
}

data := []myDataFrame{
	{0, 0, 0},
	{1, 1, 0.1},
	{2, 2, 0.2},
}

// Create the new DataFrame
df, err := NewDataFrameFromStruct(data)

Valid Types

In the struct fields only are valid the next basic types:

  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • float32
  • float64
  • Complex64
  • complex128
  • string

Also it can use a struct, if it implements the Values interface:

  • IntType
  • UintType
  • FloatType
  • ComplexType
  • StringType

Example:

// Custom struct
type Thousand struct {
	value float64
}

// Implements the FloatType interface
func (t *Thousand) String() {
	return fmt.Sprintf("%g", f.v)
}

func (f simpleFloatType) Value() float64 {
	return f.v / 1000
}

func (f simpleFloatType) Compare(v float64) Comparers {
	if f.v == v {
		return EQUAL
	} else if f.v < v {
		return LESS
	}

	return GREAT
}

// DataFrame struct
type MyStruct {
	Country string `colname:"country"`
	// Custom type
	People Thousand `colname:"people"`
}

df, _ := NewDataFrameFromStruct(data)

Index

Constants

View Source
const (
	INT     columnType = "int"
	UINT    columnType = "uint"
	FLOAT   columnType = "float"
	COMPLEX columnType = "complex"
	STRING  columnType = "string"
)

Constans with the valid basic types for the columns.

View Source
const (
	ASC  orderType = 1
	DESC orderType = 2
)

The valid order types.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseType

type BaseType interface {
	// String returns the DataFrame value stored in the struct as string.
	String() string
}

BaseType interface is the base interface of the *ValueTypes* interfaces. These interfaces are used for implements custom types for the DataFrame columns. There are one *ValueTypes* interface for each valid basic type:

  • IntType
  • UintType
  • FloatType
  • ComplexType
  • StringType

Example:

// Float custom type.
type Kelvin struct {
	value float64
}

// implements the interface IntType

func (k *Kelvin) String() string {
	return fmt.Sprintf("%g", k.value + 273)
}

func (c *Kelvin) Value() float64 {
	return c.value + 273
}

func (c *Kelvin) Compare(v float64) Comparers {
	if c.value == v {
		return EQUAL
	}

	if c.value < v {
		return LESS
	}

	return GREAT
}

type Comparers

type Comparers int8

Comparers is the variable type that returns the Compare functions in The *ValueTypes*

const (
	LESS  Comparers = -1
	EQUAL Comparers = 0
	GREAT Comparers = 1
)

Values returned by the compare functions.

type ComplexType

type ComplexType interface {
	BaseType
	// Value returns the DataFrame value stored in the struct as complex.
	Value() complex128
	// Compare compare the DataFrame value stored in the struct with the param.
	Compare(v complex128) Comparers
}

ComplexType interface is used to create custom complex types for the DataFrame columns.

type CsvConfig

type CsvConfig struct {
	// contains the csv column separator.
	Comma rune
	// True to use \r\n as line terminator.
	UseCRLF bool
	// DataFrame column names will be exported.
	Columns []string
	// Range of DataFrame rows will be exported.
	Range CsvRowRange
}

CsvConfig struct is used to define the options to export the DataFrame in a csv file.

type CsvRowRange

type CsvRowRange struct {
	Min int // Min Dataframe row position.
	Max int // Max Dataframe row position.
}

CsvRowRange struct is used to define the range of rows in DataFrame that will be exported in csv. It is similar to range in go slices.

type DataFrame

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

DataFrame struct is the main struct in the package. It provides a set of methods to get and manipulate all data in dataframe.

func NewDataFrameFromStruct

func NewDataFrameFromStruct(data interface{}) (*DataFrame, error)

NewDataFrameFromStruct creates a new DataFrame using a struct array as data.

func (*DataFrame) Column

func (df *DataFrame) Column(colname string) ([]Value, error)

Column returns the values of a DataFrame column in an array. Returns an error if the column does not exists.

func (*DataFrame) ColumnAsComplex

func (df *DataFrame) ColumnAsComplex(colname string) ([]complex128, error)

ColumnAsComplex returns the colName column as an array of complex numbers.

func (*DataFrame) ColumnAsComplexRange

func (df *DataFrame) ColumnAsComplexRange(colname string, min, max int) ([]complex128, error)

ColumnAsComplexRange returns the values between the rows min and max of the colName column as an array of complex numbers.

func (*DataFrame) ColumnAsFloat

func (df *DataFrame) ColumnAsFloat(colname string) ([]float64, error)

ColumnAsFloat returns the colName column as an array of floats.

func (*DataFrame) ColumnAsFloatRange

func (df *DataFrame) ColumnAsFloatRange(colname string, min, max int) ([]float64, error)

ColumnAsFloatRange returns the values between the rows min and max of the colName column as an array of floats.

func (*DataFrame) ColumnAsInt

func (df *DataFrame) ColumnAsInt(colname string) ([]int64, error)

ColumnAsInt returns the colName column as an array of integers.

func (*DataFrame) ColumnAsIntRange

func (df *DataFrame) ColumnAsIntRange(colname string, min, max int) ([]int64, error)

ColumnAsIntRange returns the values between the rows min and max of the colName column as an array of integers.

func (*DataFrame) ColumnAsString

func (df *DataFrame) ColumnAsString(colname string) ([]string, error)

ColumnAsString returns the colName column as an array of strings.

func (*DataFrame) ColumnAsStringRange

func (df *DataFrame) ColumnAsStringRange(colname string, min, max int) ([]string, error)

ColumnAsStringRange returns the values between the rows min and max of the colName column as an array of strings.

func (*DataFrame) ColumnAsUint

func (df *DataFrame) ColumnAsUint(colname string) ([]uint64, error)

ColumnAsUint returns the colName column as an array of unsigned integers.

func (*DataFrame) ColumnAsUintRange

func (df *DataFrame) ColumnAsUintRange(colname string, min, max int) ([]uint64, error)

ColumnAsUintRange returns the values between the rows min and max of the colName column as an array of unsinged integers.

func (*DataFrame) ColumnRange

func (df *DataFrame) ColumnRange(colname string, min, max int) ([]Value, error)

ColumnRange returns a values range of a DataFrame column in an array. Returns an error if the column does not exists or the range index is invalid.

func (*DataFrame) ExportCsvFile

func (df *DataFrame) ExportCsvFile(f *os.File, conf *CsvConfig) error

ExportCsvFile exports the DataFrame rows in the f file as a csv file, using the conf config.

func (*DataFrame) ExportCsvFileDefault

func (df *DataFrame) ExportCsvFileDefault(f *os.File) error

ExportCsvFileDefault exports the DataFrame rows in the f file as a csv file, using the default config.

  • Comma: ; character,
  • UseCRLF: false
  • Columns: All columns
  • Range: All rows

func (*DataFrame) Headers

func (df *DataFrame) Headers() []string

Headers returns the columns header of dataframe.

func (*DataFrame) Iterator

func (df *DataFrame) Iterator() *Iterator

Iterator returns a Iterator to access the data rows sequentially.

func (*DataFrame) IteratorRange

func (df *DataFrame) IteratorRange(min, max int) (*Iterator, error)

IteratorRange creates a new Iterator with the range specified in the parameters. returns an error if the range values (min or max) are invalid.

func (*DataFrame) Max

func (df *DataFrame) Max(colName string) (interface{}, error)

Max returns the max of the colName DataFrame column,

func (*DataFrame) MaxRange

func (df *DataFrame) MaxRange(colName string, min, max int) (interface{}, error)

MaxRange returns the max of the colName DataFrame column, in the range rows between min or max parameters.

func (*DataFrame) Min

func (df *DataFrame) Min(colName string) (interface{}, error)

Min returns the max of the colName DataFrame column,

func (*DataFrame) MinRange

func (df *DataFrame) MinRange(colName string, min, max int) (interface{}, error)

MinRange returns the max of the colName DataFrame column, in the range rows between min or max parameters.

func (*DataFrame) NumberRows

func (df *DataFrame) NumberRows() int

NumberRows returns the number of rows in DataFrame.

func (*DataFrame) Operation

func (df *DataFrame) Operation(op Operation) error

Operation execxute the func operation in all DataFrame rows.

func (*DataFrame) OperationRange

func (df *DataFrame) OperationRange(op Operation, min, max int) error

Operation Execute the func operation using the DataFrame rows between min and max

func (*DataFrame) Order

func (df *DataFrame) Order(newOrder ...OrderColumn) error

Order orders the DataFrame rows using the newOrder array. Returns an error if the column name is not exists.

func (*DataFrame) Sum

func (df *DataFrame) Sum(colname string) (interface{}, error)

Sum sum all values of te column colName. Only it can use the function with columns that has a valid type. The value returned will depend of the colum type:

  • int int64
  • uint uint64
  • float float32
  • complex complex128

func (*DataFrame) SumRange

func (df *DataFrame) SumRange(colName string, min, max int) (interface{}, error)

Sum sum values of the column colName, between rows min and max. Only it can use the function with columns that has a valid type. The value returned will depend of the colum type:

  • int int64
  • uint uint64
  • float float32
  • complex complex128

type DataHandler

type DataHandler interface {
	// Get returns the DataFrame Value of the row and column that match
	// with the function params.
	Get(row int, column string) (Value, error)
	// Len returns the DataFrame rows number.
	Len() int
	// Order the DataFrame rows in function of the DataFrame order field.
	Order() error
}

DataHandler interface it is used to manipulate the data of DataFrame. It is designed to create different input data type for the DataFrame (struct, csv, ...)

type ErrorCsvFile

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

ErrorCsvFile is a struct to define the errors exporting the DataFrame in a csv file.

func (*ErrorCsvFile) Error

func (e *ErrorCsvFile) Error() string

func (*ErrorCsvFile) String

func (e *ErrorCsvFile) String() string

String returns the error as a string.

type FloatType

type FloatType interface {
	BaseType
	// Value returns the DataFrame value stored in the struct as float.
	Value() float64
	// Compare compare the DataFrame value stored in the struct with the param.
	Compare(v float64) Comparers
}

FloatType interface is used to create custom float types for the DataFrame columns.

type IntType

type IntType interface {
	BaseType
	// Value returns the DataFrame value stored in the struct as int.
	Value() int64
	// Compare compare the DataFrame value stored in the struct with the param.
	Compare(v int64) Comparers
}

IntType interface is used to create custom int types for the DataFrame columns.

type Iterator

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

Iterator struct allows iterate the DataFrame rows and access to each of the row data orderly

Example:

data := struct{
	A int `colName:"a"`,
	B float32 `colName:"b"`
}{
	{1, 3.1},
	{2, 3.2},
	{3, 3.3},
}

df, err := NewDataFrameFromStruct(data)
iter := df.Iterator()

for row, cont := iter.Next(); cont; row, cont = iter.Next() {
	// proccess the row
}

func (*Iterator) Current

func (it *Iterator) Current() Row

Current returns the current row.

func (*Iterator) Index

func (it *Iterator) Index() int

Index returns the Iterator index.

func (*Iterator) Next

func (it *Iterator) Next() (Row, bool)

Next returns the current row of the iterator and advance one position. Whether the iterator can not advance more (is in the last row), then it returns false as second argument.

func (*Iterator) Position

func (it *Iterator) Position() int

Position returns the current iterator position.

func (*Iterator) Reset

func (it *Iterator) Reset()

Reset resets the iterator.

type Operation

type Operation interface {
	// F Function will execute in each iteration. In each iteration it is passed the
	// current row of the DataFrame iterator.
	F(*Row) error
}

Operation interface it is used to craete custom operations with the DataFrame rows. This interface is used in combination with the DataFrame method Operation and OperationRange.

type OperationBase

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

OperationBase is the base struct for all operations.

type OperationBaseComplex

type OperationBaseComplex struct {
	OperationBase
	Total complex128 // total result of the operation.
}

OperationBaseComplex is the base for all operations type complex.

func (*OperationBaseComplex) F

func (o *OperationBaseComplex) F(r *Row) error

F sum the value of the Cell Colname, fetched from r, with the total value: Total.

type OperationBaseFloat

type OperationBaseFloat struct {
	OperationBase
	Total float64 // total result of the operation.
}

OperationBaseFloat is the base for all operations type float.

type OperationBaseInt

type OperationBaseInt struct {
	OperationBase
	Total int64 // total result of the operation.
}

OperationBaseInt is the base for all operations type int.

type OperationBaseUint

type OperationBaseUint struct {
	OperationBase
	Total uint64 // total result of the operation.
}

OperationBaseUint is the base for all operations type uint.

type OperationComplexMinOrMax

type OperationComplexMinOrMax struct {
	OperationBaseComplex
	// contains filtered or unexported fields
}

OperationIntMinOrMax is a struct that calculates the min or the max of a Dataframe column of type complex

func (*OperationComplexMinOrMax) F

F checks if column value in the row is more great or less than the value of the struct.

type OperationFloatMinOrMax

type OperationFloatMinOrMax struct {
	OperationBaseFloat
	// contains filtered or unexported fields
}

OperationIntMinOrMax is a struct that calculates the min or the max of a Dataframe column of type float

func (*OperationFloatMinOrMax) F

func (o *OperationFloatMinOrMax) F(r *Row) error

F checks if column value in the row is more great or less than the value of the struct.

type OperationIntMinOrMax

type OperationIntMinOrMax struct {
	OperationBaseInt
	// contains filtered or unexported fields
}

OperationIntMinOrMax is a struct that calculates the min or the max of a Dataframe column of type int

func (*OperationIntMinOrMax) F

func (o *OperationIntMinOrMax) F(r *Row) error

F checks if column value in the row is more great or less than the value of the struct.

type OperationUintMinOrMax

type OperationUintMinOrMax struct {
	OperationBaseUint
	// contains filtered or unexported fields
}

OperationIntMinOrMax is a struct that calculates the min or the max of a Dataframe column of type uint

func (*OperationUintMinOrMax) F

func (o *OperationUintMinOrMax) F(r *Row) error

F checks if column value in the row is more great or less than the value of the struct.

type OperatrionSumComplex

type OperatrionSumComplex struct {
	OperationBaseComplex
}

OperatrionSumComplex is a struct used to sum all values of a DataFrame column type complex.

type OperatrionSumFloat

type OperatrionSumFloat struct {
	OperationBaseFloat
}

OperationSumFloat is a struct used to sum all values of a DataFrame column type float.

func (*OperatrionSumFloat) F

func (o *OperatrionSumFloat) F(r *Row) error

F sum the value of the Cell Colname, fetched from r, with the total value: Total.

type OperatrionSumInt

type OperatrionSumInt struct {
	OperationBaseInt
}

OperationSumInt is a struct used to sum all values of a DataFrame column type int.

func (*OperatrionSumInt) F

func (o *OperatrionSumInt) F(r *Row) error

F sum the value of the Cell Colname, fetched from r, with the total value: Total.

type OperatrionSumUint

type OperatrionSumUint struct {
	OperationBaseUint
}

OperationSumUint is a struct used to sum all values of a DataFrame column type uint.

func (*OperatrionSumUint) F

func (o *OperatrionSumUint) F(r *Row) error

F sum the value of the Cell Colname, fetched from r, with the total value: Total.

type OrderColumn

type OrderColumn struct {
	Name  string    // Column name
	Order orderType // Order type
}

OrderColumn is the struct used to define the order of the DataFrame rows.

Example:

data := struct{
	A int `colName:"a"`
	B int `colName:"b"`
}{
	{1, 1},
	{2, 2},
	{1, 2},
}

df, _ := NewDataFrameFromStruct(data)

df.Order(OrderColumn{"a", ASC}, OrderColumn{"b", DESC})

type Row

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

Row handles a row DataFrame.

func (*Row) Cell

func (r *Row) Cell(colname string) (Value, error)

Cell returns the value inside of the Row of the colname column. If the column does not exists, then returns an error.

type StringType

type StringType interface {
	BaseType
	// Value returns the DataFrame value stored in the struct as string.
	Value() string
	// Compare compare the DataFrame value stored in the struct with the param.
	Compare(v string) Comparers
}

StringType interface is used to create custom string types for the DataFrame columns.

type UintType

type UintType interface {
	BaseType
	// Value returns the DataFrame value stored in the struct as uint.
	Value() uint64
	// Compare compare the DataFrame value stored in the struct with the param.
	Compare(v uint64) Comparers
}

UintType interface is used to create custom uint types for the DataFrame columns.

type Value

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

Value is the struct where save a DataFrame cell value.

func (*Value) Complex128

func (v *Value) Complex128() (complex128, error)

Complex128 returns the value as complex128. `v.value` must has the `ComplexType` type. If not returns an error.

func (*Value) Complex64

func (v *Value) Complex64() (complex64, error)

Complex64 returns the value as complex64. `v.value` must has the `ComplexType` type. If not returns an error.

func (*Value) ComplexType

func (v *Value) ComplexType() (ComplexType, error)

ComplexType casts the v.value variable in ComplexType. It generates an error if the casting is impossible.

func (*Value) Float32

func (v *Value) Float32() (float32, error)

Float32 returns the value as float32. `v.value` must has the `FloaType` type. If not returns an error.

func (*Value) Float64

func (v *Value) Float64() (float64, error)

Float64 returns the value as float64. `v.value` must has the `FloaType` type. If not returns an error.

func (*Value) FloatType

func (v *Value) FloatType() (FloatType, error)

FloatType casts the v.value variable in FloatType. It generates an error if the casting is impossible.

func (*Value) Int

func (v *Value) Int() (int, error)

Int returns the value as int. `v.value` must has the `IntType` type. If not returns an error.

func (*Value) Int16

func (v *Value) Int16() (int16, error)

Int16 returns the value as int16. `v.value` must has the `IntType` type. If not returns an error.

func (*Value) Int32

func (v *Value) Int32() (int32, error)

Int32 returns the value as int32. `v.value` must has the `IntType` type. If not returns an error.

func (*Value) Int64

func (v *Value) Int64() (int64, error)

Int64 returns the value as int64. `v.value` must has the `IntType` type. If not returns an error.

func (*Value) Int8

func (v *Value) Int8() (int8, error)

Int8 returns the value as int8. `v.value` must has the `IntType` type. If not returns an error.

func (*Value) IntType

func (v *Value) IntType() (IntType, error)

IntType casts the v.value variable in IntType. It generates an error if the casting is impossible.

func (*Value) Str

func (v *Value) Str() (string, error)

Str casts the v.value variable in a string. v.value variable only will cast in string if is type StringType. Any else type returns an error.

func (*Value) String

func (v *Value) String() string

String casts all valid values to string and return they. If the value is not valid then throw and panic error.

func (*Value) StringType

func (v *Value) StringType() (StringType, error)

StringType casts the v.value variable in StringType. It generates an error if the casting is impossible.

func (*Value) Uint

func (v *Value) Uint() (uint, error)

Uint returns the value as uint. `v.value` must has the `UintType` type. If not returns an error.

func (*Value) Uint16

func (v *Value) Uint16() (uint16, error)

Uint16 returns the value as uint16. `v.value` must has the `UintType` type. If not returns an error.

func (*Value) Uint32

func (v *Value) Uint32() (uint32, error)

Uint32 returns the value as uint32. `v.value` must has the `UintType` type. If not returns an error.

func (*Value) Uint64

func (v *Value) Uint64() (uint64, error)

Uint64 returns the value as uint64. `v.value` must has the `UintType` type. If not returns an error.

func (*Value) Uint8

func (v *Value) Uint8() (uint8, error)

Uint8 returns the value as uint8. `v.value` must has the `UintType` type. If not returns an error.

func (*Value) UintType

func (v *Value) UintType() (UintType, error)

UintType casts the v.value variable in UintType. It generates an error if the casting is impossible.

Jump to

Keyboard shortcuts

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