Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Aggregation_FIRST and _LAST options, and used interface to support strings #218

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ func (df DataFrame) Subset(indexes series.Indexes) DataFrame {

// SelectIndexes are the supported indexes used for the DataFrame.Select method. Currently supported are:
//
// int // Matches the given index number
// []int // Matches all given index numbers
// []bool // Matches all columns marked as true
// string // Matches the column with the matching column name
// []string // Matches all columns with the matching column names
// Series [Int] // Same as []int
// Series [Bool] // Same as []bool
// Series [String] // Same as []string
// int // Matches the given index number
// []int // Matches all given index numbers
// []bool // Matches all columns marked as true
// string // Matches the column with the matching column name
// []string // Matches all columns with the matching column names
// Series [Int] // Same as []int
// Series [Bool] // Same as []bool
// Series [String] // Same as []string
type SelectIndexes interface{}

// Select the given DataFrame columns
Expand Down Expand Up @@ -382,7 +382,7 @@ func (df DataFrame) Drop(indexes SelectIndexes) DataFrame {

const KEY_ERROR = "KEY_ERROR"

//GroupBy Group dataframe by columns
// GroupBy Group dataframe by columns
func (df DataFrame) GroupBy(colnames ...string) *Groups {
if len(colnames) <= 0 {
return nil
Expand Down Expand Up @@ -434,7 +434,7 @@ func (df DataFrame) GroupBy(colnames ...string) *Groups {
return groups
}

//AggregationType Aggregation method type
// AggregationType Aggregation method type
type AggregationType int

//go:generate stringer -type=AggregationType -linecomment
Expand All @@ -446,9 +446,11 @@ const (
Aggregation_STD // STD
Aggregation_SUM // SUM
Aggregation_COUNT // COUNT
Aggregation_FIRST // FIRST
Aggregation_LAST // LAST
)

//Groups : structure generated by groupby
// Groups : structure generated by groupby
type Groups struct {
groups map[string]DataFrame
colnames []string
Expand All @@ -468,7 +470,7 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra
for _, df := range gps.groups {
targetMap := df.Maps()[0]
curMap := make(map[string]interface{})
// add columns of group by
// add columns of group by
for _, c := range gps.colnames {
if value, ok := targetMap[c]; ok {
curMap[c] = value
Expand All @@ -479,7 +481,7 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra
// Aggregation
for i, c := range colnames {
curSeries := df.Col(c)
var value float64
var value interface{}
switch typs[i] {
case Aggregation_MAX:
value = curSeries.Max()
Expand All @@ -495,11 +497,16 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra
value = curSeries.Sum()
case Aggregation_COUNT:
value = float64(curSeries.Len())
case Aggregation_FIRST:
value = curSeries.Elem(0)
case Aggregation_LAST:
value = curSeries.Elem(curSeries.Len() - 1)
default:
return DataFrame{Err: fmt.Errorf("Aggregation: this method %s not found", typs[i])}

}
curMap[fmt.Sprintf("%s_%s", c, typs[i])] = value
//curMap[(c)] = value
}
dfMaps = append(dfMaps, curMap)

Expand Down Expand Up @@ -1039,23 +1046,23 @@ func WithComments(b rune) LoadOption {
//
// Examples:
//
// // field will be ignored
// field int
// // field will be ignored
// field int
//
// // Field will be ignored
// Field int `dataframe:"-"`
// // Field will be ignored
// Field int `dataframe:"-"`
//
// // Field will be parsed with column name Field and type int
// Field int
// // Field will be parsed with column name Field and type int
// Field int
//
// // Field will be parsed with column name `field_column` and type int.
// Field int `dataframe:"field_column"`
// // Field will be parsed with column name `field_column` and type int.
// Field int `dataframe:"field_column"`
//
// // Field will be parsed with column name `field` and type string.
// Field int `dataframe:"field,string"`
// // Field will be parsed with column name `field` and type string.
// Field int `dataframe:"field,string"`
//
// // Field will be parsed with column name `Field` and type string.
// Field int `dataframe:",string"`
// // Field will be parsed with column name `Field` and type string.
// Field int `dataframe:",string"`
//
// If the struct tags and the given LoadOptions contradict each other, the later
// will have preference over the former.
Expand Down