- Notifications
You must be signed in to change notification settings - Fork13
datasweet/datatable
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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
go get github.com/datasweet/datatable
- Create custom Series (ie custom columns). Currently available, serie.Int, serie.String, serie.Time, serie.Float64.
- Apply expressions
- Selects (head, tail, subset)
- Sorting
- InnerJoin, LeftJoin, RightJoin, OuterJoin, Concats
- Aggregate
- Import from CSV
- Export to map, slice
package mainimport ("fmt""github.com/datasweet/datatable")funcmain() {dt:=datatable.New("test")dt.AddColumn("champ",datatable.String,datatable.Values("Malzahar","Xerath","Teemo"))dt.AddColumn("champion",datatable.String,datatable.Expr("upper(`champ`)"))dt.AddColumn("win",datatable.Int,datatable.Values(10,20,666))dt.AddColumn("loose",datatable.Int,datatable.Values(6,5,666))dt.AddColumn("winRate",datatable.Float64,datatable.Expr("`win` * 100 / (`win` + `loose`)"))dt.AddColumn("winRate %",datatable.String,datatable.Expr(" `winRate` ~\" %\""))dt.AddColumn("sum",datatable.Float64,datatable.Expr("sum(`win`)"))fmt.Println(dt)}/*CHAMP <NULLSTRING> CHAMPION <NULLSTRING> WIN <NULLINT> LOOSE <NULLINT> WINRATE <NULLFLOAT64> WINRATE % <NULLSTRING> SUM <NULLFLOAT64>Malzahar MALZAHAR 10 6 62.5 62.5 % 696Xerath XERATH 20 5 80 80 % 696Teemo TEEMO 666 666 50 50 % 696*/
package mainimport ("fmt""log""os""time""github.com/datasweet/datatable""github.com/datasweet/datatable/import/csv")funcmain() {dt,err:=csv.Import("csv","phone_data.csv",csv.HasHeader(true),csv.AcceptDate("02/01/06 15:04"),csv.AcceptDate("2006-01"),)iferr!=nil {log.Fatalf("reading csv: %v",err)}dt.Print(os.Stdout,datatable.PrintMaxRows(24))dt2,err:=dt.Aggregate(datatable.AggregateBy{Type:datatable.Count,Field:"index"})iferr!=nil {log.Fatalf("aggregate COUNT('index'): %v",err)}fmt.Println(dt2)groups,err:=dt.GroupBy(datatable.GroupBy{Name:"year",Type:datatable.Int,Keyer:func(row datatable.Row) (interface{},bool) {ifd,ok:=row["date"];ok {iftm,ok:=d.(time.Time);ok {returntm.Year(),true}}returnnil,false},})iferr!=nil {log.Fatalf("GROUP BY 'year': %v",err)}dt3,err:=groups.Aggregate(datatable.AggregateBy{Type:datatable.Sum,Field:"duration"},datatable.AggregateBy{Type:datatable.CountDistinct,Field:"network"},)iferr!=nil {log.Fatalf("Aggregate SUM('duration'), COUNT_DISTINCT('network') GROUP BY 'year': %v",err)}fmt.Println(dt3)}
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 IntNfuncIntN(v...interface{})Serie {s,_:=New(NullInt{},asNullInt,compareNullInt)iflen(v)>0 {s.Append(v...)}returns}typeNullIntstruct {IntintValidbool}// 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 (iNullInt)Interface()interface{} {ifi.Valid {returni.Int}returnnil}// asNullInt is our caster functionfuncasNullInt(iinterface{})NullInt {varniNullIntifi==nil {returnni}ifv,ok:=i.(NullInt);ok {returnv}ifv,err:=cast.ToIntE(i);err==nil {ni.Int=vni.Valid=true}returnni}// compareNullInt is our comparator function// used to sortfunccompareNullInt(a,bNullInt)int {if!b.Valid {if!a.Valid {returnEq}returnGt}if!a.Valid {returnLt }ifa.Int==b.Int {returnEq}ifa.Int<b.Int {returnLt}returnGt}
We are Datasweet, a french startup providing full service (big) data solutions.
If you find a bug or want to request a feature, please create aGitHub Issue.
Cléo Rebert |
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 notuse this file except in compliance with the License. You may obtain a copy ofthe License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS, WITHOUTWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See theLicense for the specific language governing permissions and limitations underthe License.