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

Feat: add PROD operation in series and aggregation #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions dataframe/aggregationtype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ const (
Aggregation_STD // STD
Aggregation_SUM // SUM
Aggregation_COUNT // COUNT
Aggregation_PROD // PROD
)

//Groups : structure generated by groupby
Expand Down Expand Up @@ -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_PROD:
value = curSeries.Prod()
default:
return DataFrame{Err: fmt.Errorf("Aggregation: this method %s not found", typs[i])}

Expand Down
3 changes: 2 additions & 1 deletion dataframe/dataframe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2972,7 +2972,8 @@ func TestDataFrame_Aggregation(t *testing.T) {
series.New([]float64{3.0, 4.0, 5.3, 3.2, 1.2}, series.Float, "values2"),
)
groups := a.GroupBy("key1", "key2")
df := groups.Aggregation([]AggregationType{Aggregation_MAX, Aggregation_MIN, Aggregation_COUNT, Aggregation_SUM}, []string{"values", "values2", "values2", "values2"})
df := groups.Aggregation([]AggregationType{Aggregation_MAX, Aggregation_MIN, Aggregation_COUNT,
Aggregation_SUM, Aggregation_PROD}, []string{"values", "values2", "values2", "values2", "values2"})
resultMap := make(map[string]float32, 3)
resultMap[fmt.Sprintf("%s_%d", "a", 2)] = 4
resultMap[fmt.Sprintf("%s_%d", "b", 1)] = 5.3
Expand Down
14 changes: 14 additions & 0 deletions series/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,20 @@ func (s Series) Sum() float64 {
return sum
}

// Prod calculates the product value of a series
func (s Series) Prod() float64 {
if s.elements.Len() == 0 || s.Type() == String || s.Type() == Bool {
return math.NaN()
}
sFloat := s.Float()
prod := sFloat[0]
for i := 1; i < len(sFloat); i++ {
elem := sFloat[i]
prod *= elem
}
return prod
}

// Slice slices Series from j to k-1 index.
func (s Series) Slice(j, k int) Series {
if s.Err != nil {
Expand Down
45 changes: 45 additions & 0 deletions series/series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,51 @@ func TestSeries_Sum(t *testing.T) {
}
}

func TestSeries_Prod(t *testing.T) {
tests := []struct {
series Series
expected float64
}{
{
// Extreme observations should not factor in.
Ints([]int{1, 2, 3, 4, 5, 10}),
1200,
},
{
// Change in order should not influence result.
Ints([]int{1, 2, 3, 4, 10, 5}),
1200,
},
{
Floats([]float64{3.1415926, -1.0, 2.33333}),
-7.330372261358001,
},
{
Strings([]string{"A", "B", "C", "D"}),
math.NaN(),
},
{
Bools([]bool{true, true, false, true}),
math.NaN(),
},
{
Floats([]float64{}),
math.NaN(),
},
}

for testnum, test := range tests {
received := test.series.Prod()
expected := test.expected
if !compareFloats(received, expected, 6) {
t.Errorf(
"Test:%v\nExpected:\n%v\nReceived:\n%v",
testnum, expected, received,
)
}
}
}

func TestSeries_Slice(t *testing.T) {
seriesWithErr := Ints([]int{})
seriesWithErr.Err = fmt.Errorf("slice index out of bounds")
Expand Down