Closed vlamai closed 6 years ago
Based on your PR, I can see that your intention is to transform all the values in the series to the Absolute value.
The way to do it is actually like this:
s1 := dataframe.NewSeriesInt64("day", nil, 1, 2, 3, 4, 5, 6, 7, 8)
s1.Lock()
n := s1.NRows(Options{DontLock:true})
for row := 0; row < n; row++ {
val := s1.Value(row, Options{DontLock:true})
if val == nil { continue }
s1.Update(row, abs(val), Options{DontLock:true})
}
s1.Unlock()
You can make this into a function of your own.
My opinion is that having an Abs()
function for a series doesn't make sense to many types of data types such as strings and dates and perhaps may more. It really only makes sense for numbers.
Do you have any good reasons for including it into the API? Maybe you have some good reasons.
How do you think need try to add all this functions ? pandas Or you have some priorities to add functions ?
I can see from the pandas API: http://pandas.pydata.org/pandas-docs/stable/api.html#computations-descriptive-stats
I think since these are only applicable to SeriesInt64
and SeriesFloat64
.
Abs
.SeriesInt64
and SeriesFloat64
which embeds the original SeriesInt64
This will allow the main package to contain only the bare essentials.
What do you think of this idea?
I also want to add to the main package: Filter
, Map
and Reduce
and use https://github.com/robpike/filter for inspiration.
This is created by the co-creator of Go and can work with any type (including time etc)
https://godoc.org/gonum.org/v1/gonum/stat can be leveraged for statistics.
I'm obviously doing something wrong. create new package stat |-stat |--stat.go
package stat
import (
"github.com/rocketlaunchr/dataframe-go"
"math"
)
type Stat interface {
dataframe.Series
Abs()
}
type SeriesFloat64 struct {
dataframe.SeriesFloat64
}
type SeriesInt64 struct {
dataframe.SeriesInt64
}
func (s *SeriesFloat64) Abs() {
for key, value := range s.Values {
val := math.Abs(*value)
s.Values[key] = &val
}
}
func (s *SeriesInt64) Abs() {
for key, value := range s.Values {
val := *value
if val < 0 {
val = val * -1
}
s.Values[key] = &val
}
}
but in stat_test.go i can't call function
package stat
import (
"github.com/rocketlaunchr/dataframe-go"
"testing"
)
func TestSeriesFloat64_Abs(t *testing.T) {
s1 := dataframe.NewSeriesFloat64("num",nil,-1.0,1.0)
s1.Abs() // error
}
can you create a sample ?
You are very close.
The error is because s1
doesn't have the Abs
method. s1
is from the dataframe
package.
It'll be something like this:
s1 := SeriesFloat64{ dataframe.NewSeriesFloat64("num",nil,-1.0,1.0) }
s1.Abs()
Also remember to check for nil
values in your Abs( )
function.
You will also need to lock the series before you manipulate any of the contained values. (since your intention is to do an InPlace manipulation).
Something like this:
func (s *SeriesFloat64) Abs() {
s.Lock()
defer s.Unlock()
for key := range s.Values {
val := s.Values[key]
if val != nil && *val < 0 {
s.Values[key] = &[]float64{-1*val}[0]
}
}
}
If it's hard make an example of a function. My knowledge is apparently not enough
What are you trying to achieve?
Also embed the pointer:
type SeriesFloat64 struct {
*dataframe.SeriesFloat64
}
type SeriesInt64 struct {
*dataframe.SeriesInt64
}
test pull requests. Add Abs function to float and int series