From d6b87ec8262fe6e563906c97a7584cde3c381027 Mon Sep 17 00:00:00 2001 From: John Moreau Date: Sun, 6 Aug 2023 14:42:41 -0700 Subject: [PATCH 1/5] Added aggregation_FIRST const and switch --- dataframe/dataframe.go | 49 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index 51d38f6..11fb7d5 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -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 @@ -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 @@ -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 @@ -446,9 +446,10 @@ const ( Aggregation_STD // STD Aggregation_SUM // SUM Aggregation_COUNT // COUNT + Aggregation_FIRST // FIRST ) -//Groups : structure generated by groupby +// Groups : structure generated by groupby type Groups struct { groups map[string]DataFrame colnames []string @@ -495,6 +496,8 @@ 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).Float() default: return DataFrame{Err: fmt.Errorf("Aggregation: this method %s not found", typs[i])} @@ -1039,23 +1042,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. From 6703f8d725f3085638f768594f46f129680e32b4 Mon Sep 17 00:00:00 2001 From: John Moreau Date: Sun, 6 Aug 2023 14:46:52 -0700 Subject: [PATCH 2/5] Changed value in aggregation to interface --- dataframe/dataframe.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index 11fb7d5..78fa554 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -469,7 +469,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 @@ -480,7 +480,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() @@ -497,7 +497,7 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra case Aggregation_COUNT: value = float64(curSeries.Len()) case Aggregation_FIRST: - value = curSeries.Elem(0).Float() + value = curSeries.Elem(0) default: return DataFrame{Err: fmt.Errorf("Aggregation: this method %s not found", typs[i])} From ab664617a4bd5a31b741e52a629be65f17c6c766 Mon Sep 17 00:00:00 2001 From: John Moreau Date: Sun, 6 Aug 2023 15:24:41 -0700 Subject: [PATCH 3/5] Check range --- dataframe/dataframe.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index 78fa554..3d64514 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -497,12 +497,15 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra case Aggregation_COUNT: value = float64(curSeries.Len()) case Aggregation_FIRST: - value = curSeries.Elem(0) + if curSeries.Len() > 0 { + value = curSeries.Elem(0) + } 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) From 0cd092f3ed97a76b07dae3ca200733518ed334c0 Mon Sep 17 00:00:00 2001 From: John Moreau Date: Sun, 6 Aug 2023 15:38:27 -0700 Subject: [PATCH 4/5] removed if check from switch --- dataframe/dataframe.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index 3d64514..c5b4f47 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -497,9 +497,7 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra case Aggregation_COUNT: value = float64(curSeries.Len()) case Aggregation_FIRST: - if curSeries.Len() > 0 { - value = curSeries.Elem(0) - } + value = curSeries.Elem(0) default: return DataFrame{Err: fmt.Errorf("Aggregation: this method %s not found", typs[i])} From bdb9e86d6c3132b831f039484f195f6574b33dc5 Mon Sep 17 00:00:00 2001 From: John Moreau Date: Tue, 8 Aug 2023 23:59:29 -0700 Subject: [PATCH 5/5] Added _LAST aggregation --- dataframe/dataframe.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index c5b4f47..c4ee5da 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -447,6 +447,7 @@ const ( Aggregation_SUM // SUM Aggregation_COUNT // COUNT Aggregation_FIRST // FIRST + Aggregation_LAST // LAST ) // Groups : structure generated by groupby @@ -498,6 +499,8 @@ func (gps Groups) Aggregation(typs []AggregationType, colnames []string) DataFra 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])}