From 999c18da954c1f386e30013100255766b980e9d9 Mon Sep 17 00:00:00 2001 From: Manish Champaneri Date: Mon, 31 Aug 2020 23:20:05 +0530 Subject: [PATCH] AddConstant: AddConstatnt function adds new named column to dataframe with provided constant value. --- dataframe/dataframe.go | 28 ++++++++++++++++++++++++++++ dataframe/dataframe_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/dataframe/dataframe.go b/dataframe/dataframe.go index cf1ae41..b0e6b05 100644 --- a/dataframe/dataframe.go +++ b/dataframe/dataframe.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "io" + "log" "reflect" "sort" "strconv" @@ -1971,3 +1972,30 @@ func (df DataFrame) Describe() DataFrame { ddf := New(ss...) return ddf } + +// AddConstant returns new dataframe with new column +// having constant value with given column name +func (df DataFrame) AddConstant(value interface{}, colName string) DataFrame { + + var typeOfSeries series.Type + switch value.(type) { + case int: + typeOfSeries = series.Int + case float64: + typeOfSeries = series.Float + case string: + typeOfSeries = series.String + case bool: + typeOfSeries = series.Bool + default: + log.Fatalln("Unsupported series type") + } + + constSlice := make([]interface{}, df.Nrow()) + for i := range constSlice { + constSlice[i] = value + } + + constSeries := series.New(constSlice, typeOfSeries, colName) + return df.Mutate(constSeries) +} diff --git a/dataframe/dataframe_test.go b/dataframe/dataframe_test.go index 114c0e4..4dfe325 100644 --- a/dataframe/dataframe_test.go +++ b/dataframe/dataframe_test.go @@ -2,6 +2,7 @@ package dataframe import ( "bytes" + "math/rand" "reflect" "strconv" "strings" @@ -68,6 +69,29 @@ func TestDataFrame_Copy(t *testing.T) { } } +func TestAddConstant(t *testing.T) { + a := New( + series.New([]string{"b", "a", "b", "c", "d"}, series.String, "COL.1"), + series.New([]int{1, 2, 4, 5, 4}, series.Int, "COL.2"), + series.New([]float64{3.0, 4.0, 5.3, 3.2, 1.2}, series.Float, "COL.3"), + ) + b := a.AddConstant(20, "f") + + colF := b.Col("f") + if colF.Err != nil { + t.Errorf("Test: AddConstant %v", colF.Err.Error()) + } else if len(b.columns) != 4 { + t.Errorf("Test: AddConstant %v", "column not added to dataframe") + } else if colF.Type() != "int" { + t.Errorf("Test: AddConstant %v", "wrong column type") + } else if colF.Len() != a.Nrow() { + t.Errorf("Test: AddConstant %v", "serise lenght not matching") + } else if a, _ := colF.Elem(rand.Intn(a.Nrow())).Int(); a != 20 { + // As column type check has passed return will be integer + // so, we are only checking for the value and error is ignored + t.Errorf("Test: AddConstant %v", "values not same for all elements.") + } +} func TestDataFrame_Subset(t *testing.T) { a := New( series.New([]string{"b", "a", "b", "c", "d"}, series.String, "COL.1"),