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
- type BaseType
- type Comparers
- type ComplexType
- type CsvConfig
- type CsvRowRange
- type DataFrame
- func (df *DataFrame) Column(colname string) ([]Value, error)
- func (df *DataFrame) ColumnAsComplex(colname string) ([]complex128, error)
- func (df *DataFrame) ColumnAsComplexRange(colname string, min, max int) ([]complex128, error)
- func (df *DataFrame) ColumnAsFloat(colname string) ([]float64, error)
- func (df *DataFrame) ColumnAsFloatRange(colname string, min, max int) ([]float64, error)
- func (df *DataFrame) ColumnAsInt(colname string) ([]int64, error)
- func (df *DataFrame) ColumnAsIntRange(colname string, min, max int) ([]int64, error)
- func (df *DataFrame) ColumnAsString(colname string) ([]string, error)
- func (df *DataFrame) ColumnAsStringRange(colname string, min, max int) ([]string, error)
- func (df *DataFrame) ColumnAsUint(colname string) ([]uint64, error)
- func (df *DataFrame) ColumnAsUintRange(colname string, min, max int) ([]uint64, error)
- func (df *DataFrame) ColumnRange(colname string, min, max int) ([]Value, error)
- func (df *DataFrame) ExportCsvFile(f *os.File, conf *CsvConfig) error
- func (df *DataFrame) ExportCsvFileDefault(f *os.File) error
- func (df *DataFrame) Headers() []string
- func (df *DataFrame) Iterator() *Iterator
- func (df *DataFrame) IteratorRange(min, max int) (*Iterator, error)
- func (df *DataFrame) Max(colName string) (interface{}, error)
- func (df *DataFrame) MaxRange(colName string, min, max int) (interface{}, error)
- func (df *DataFrame) Min(colName string) (interface{}, error)
- func (df *DataFrame) MinRange(colName string, min, max int) (interface{}, error)
- func (df *DataFrame) NumberRows() int
- func (df *DataFrame) Operation(op Operation) error
- func (df *DataFrame) OperationRange(op Operation, min, max int) error
- func (df *DataFrame) Order(newOrder ...OrderColumn) error
- func (df *DataFrame) Sum(colname string) (interface{}, error)
- func (df *DataFrame) SumRange(colName string, min, max int) (interface{}, error)
- type DataHandler
- type ErrorCsvFile
- type FloatType
- type IntType
- type Iterator
- type Operation
- type OperationBase
- type OperationBaseComplex
- type OperationBaseFloat
- type OperationBaseInt
- type OperationBaseUint
- type OperationComplexMinOrMax
- type OperationFloatMinOrMax
- type OperationIntMinOrMax
- type OperationUintMinOrMax
- type OperatrionSumComplex
- type OperatrionSumFloat
- type OperatrionSumInt
- type OperatrionSumUint
- type OrderColumn
- type Row
- type StringType
- type UintType
- type Value
- func (v *Value) Complex128() (complex128, error)
- func (v *Value) Complex64() (complex64, error)
- func (v *Value) ComplexType() (ComplexType, error)
- func (v *Value) Float32() (float32, error)
- func (v *Value) Float64() (float64, error)
- func (v *Value) FloatType() (FloatType, error)
- func (v *Value) Int() (int, error)
- func (v *Value) Int16() (int16, error)
- func (v *Value) Int32() (int32, error)
- func (v *Value) Int64() (int64, error)
- func (v *Value) Int8() (int8, error)
- func (v *Value) IntType() (IntType, error)
- func (v *Value) Str() (string, error)
- func (v *Value) String() string
- func (v *Value) StringType() (StringType, error)
- func (v *Value) Uint() (uint, error)
- func (v *Value) Uint16() (uint16, error)
- func (v *Value) Uint32() (uint32, error)
- func (v *Value) Uint64() (uint64, error)
- func (v *Value) Uint8() (uint8, error)
- func (v *Value) UintType() (UintType, error)
Constants ¶
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.
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*
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 ¶
NewDataFrameFromStruct creates a new DataFrame using a struct array as data.
func (*DataFrame) Column ¶
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 ¶
ColumnAsFloat returns the colName column as an array of floats.
func (*DataFrame) ColumnAsFloatRange ¶
ColumnAsFloatRange returns the values between the rows min and max of the colName column as an array of floats.
func (*DataFrame) ColumnAsInt ¶
ColumnAsInt returns the colName column as an array of integers.
func (*DataFrame) ColumnAsIntRange ¶
ColumnAsIntRange returns the values between the rows min and max of the colName column as an array of integers.
func (*DataFrame) ColumnAsString ¶
ColumnAsString returns the colName column as an array of strings.
func (*DataFrame) ColumnAsStringRange ¶
ColumnAsStringRange returns the values between the rows min and max of the colName column as an array of strings.
func (*DataFrame) ColumnAsUint ¶
ColumnAsUint returns the colName column as an array of unsigned integers.
func (*DataFrame) ColumnAsUintRange ¶
ColumnAsUintRange returns the values between the rows min and max of the colName column as an array of unsinged integers.
func (*DataFrame) ColumnRange ¶
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 ¶
ExportCsvFile exports the DataFrame rows in the f file as a csv file, using the conf config.
func (*DataFrame) ExportCsvFileDefault ¶
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) IteratorRange ¶
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) MaxRange ¶
MaxRange returns the max of the colName DataFrame column, in the range rows between min or max parameters.
func (*DataFrame) MinRange ¶
MinRange returns the max of the colName DataFrame column, in the range rows between min or max parameters.
func (*DataFrame) NumberRows ¶
NumberRows returns the number of rows in DataFrame.
func (*DataFrame) OperationRange ¶
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 ¶
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
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) Next ¶
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.
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 ¶
func (o *OperationComplexMinOrMax) F(r *Row) error
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.
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 ¶
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 ¶
Float32 returns the value as float32. `v.value` must has the `FloaType` type. If not returns an error.
func (*Value) Float64 ¶
Float64 returns the value as float64. `v.value` must has the `FloaType` type. If not returns an error.
func (*Value) FloatType ¶
FloatType casts the v.value variable in FloatType. It generates an error if the casting is impossible.
func (*Value) Int ¶
Int returns the value as int. `v.value` must has the `IntType` type. If not returns an error.
func (*Value) Int16 ¶
Int16 returns the value as int16. `v.value` must has the `IntType` type. If not returns an error.
func (*Value) Int32 ¶
Int32 returns the value as int32. `v.value` must has the `IntType` type. If not returns an error.
func (*Value) Int64 ¶
Int64 returns the value as int64. `v.value` must has the `IntType` type. If not returns an error.
func (*Value) Int8 ¶
Int8 returns the value as int8. `v.value` must has the `IntType` type. If not returns an error.
func (*Value) IntType ¶
IntType casts the v.value variable in IntType. It generates an error if the casting is impossible.
func (*Value) Str ¶
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 ¶
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 ¶
Uint returns the value as uint. `v.value` must has the `UintType` type. If not returns an error.
func (*Value) Uint16 ¶
Uint16 returns the value as uint16. `v.value` must has the `UintType` type. If not returns an error.
func (*Value) Uint32 ¶
Uint32 returns the value as uint32. `v.value` must has the `UintType` type. If not returns an error.
func (*Value) Uint64 ¶
Uint64 returns the value as uint64. `v.value` must has the `UintType` type. If not returns an error.