From 485a0e3b0b1906d40f46049912f704ce7c52ac3f Mon Sep 17 00:00:00 2001 From: wangfeng Date: Sat, 4 Feb 2023 18:25:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=8B=AC=E7=AB=8Bmean=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_rolling.go | 10 ---------- rolling_mean.go | 11 +++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 rolling_mean.go diff --git a/generic_rolling.go b/generic_rolling.go index 1859108..80d2653 100644 --- a/generic_rolling.go +++ b/generic_rolling.go @@ -54,13 +54,3 @@ func (r RollingAndExpandingMixin) getBlocks() (blocks []Series) { return } - -// Mean returns the rolling mean. -func (r RollingAndExpandingMixin) Mean() (s Series) { - var d []float64 - for _, block := range r.getBlocks() { - d = append(d, block.Mean()) - } - s = NewSeries(SERIES_TYPE_FLOAT, r.series.Name(), d) - return -} diff --git a/rolling_mean.go b/rolling_mean.go new file mode 100644 index 0000000..62dcf2c --- /dev/null +++ b/rolling_mean.go @@ -0,0 +1,11 @@ +package pandas + +// Mean returns the rolling mean. +func (r RollingAndExpandingMixin) Mean() (s Series) { + var d []float64 + for _, block := range r.getBlocks() { + d = append(d, block.Mean()) + } + s = NewSeries(SERIES_TYPE_FLOAT, r.series.Name(), d) + return +} -- Gitee From 45769cd2db5f2f086a09f11091a652b669e8c5b9 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Sat, 4 Feb 2023 19:39:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#I6CC1K=20=E5=AE=9E=E7=8E=B0DIFF=E5=8A=9F?= =?UTF-8?q?=E8=83=BD,=20=E5=8C=85=E6=8B=AC=E5=9B=BA=E5=AE=9A=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E5=BA=8F=E5=88=97=E5=8C=96=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_diff.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ generic_diff_test.go | 24 +++++++++++++++ series.go | 2 ++ 3 files changed, 95 insertions(+) create mode 100644 generic_diff.go create mode 100644 generic_diff_test.go diff --git a/generic_diff.go b/generic_diff.go new file mode 100644 index 0000000..9cc1a16 --- /dev/null +++ b/generic_diff.go @@ -0,0 +1,69 @@ +package pandas + +import ( + "gitee.com/quant1x/pandas/stat" + "reflect" +) + +//func (self *NDFrame) Diff_() float64 { +// if self.Len() < 1 { +// return NaN() +// } +// fs := make([]float64, 0) +// self.Apply(func(idx int, v any) { +// f := AnyToFloat64(v) +// fs = append(fs, f) +// }) +// stdDev := avx2.Mean(fs) +// return stdDev +//} + +// Diff 元素的第一个离散差 +// First discrete difference of element. +// Calculates the difference of a {klass} element compared with another +// element in the {klass} (default is element in previous row). +func (self *NDFrame) Diff(param any) (s Series) { + if !(self.type_ == SERIES_TYPE_INT || self.type_ == SERIES_TYPE_FLOAT) { + return NewSeries(SERIES_TYPE_INVAILD, "", "") + } + var N []float32 + switch v := param.(type) { + case int: + N = stat.Repeat[float32](float32(v), self.Len()) + case Series: + vs := v.Values() + N = sliceToFloat32(vs) + N = stat.Align(N, Nil2Float32, self.Len()) + default: + //periods = 1 + N = stat.Repeat[float32](float32(1), self.Len()) + } + r := RollingAndExpandingMixin{ + window: N, + series: self, + } + var d []float64 + var front = Nil2Float64 + for _, block := range r.getBlocks() { + vs := reflect.ValueOf(block.Values()) + vl := vs.Len() + if vl == 0 { + d = append(d, Nil2Float64) + continue + } + vf := vs.Index(0).Interface() + vc := vs.Index(vl - 1).Interface() + cu := AnyToFloat64(vc) + cf := AnyToFloat64(vf) + if Float64IsNaN(cu) || Float64IsNaN(front) { + front = cf + d = append(d, Nil2Float64) + continue + } + diff := cu - front + d = append(d, diff) + front = cf + } + s = NewSeries(SERIES_TYPE_FLOAT, r.series.Name(), d) + return +} diff --git a/generic_diff_test.go b/generic_diff_test.go new file mode 100644 index 0000000..deff2d7 --- /dev/null +++ b/generic_diff_test.go @@ -0,0 +1,24 @@ +package pandas + +import ( + "fmt" + "testing" +) + +func TestNDFrame_Diff(t *testing.T) { + d1 := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} + s1 := NewNDFrame[float64]("x", d1...) + df := NewDataFrame(s1) + fmt.Println(df) + fmt.Println("------------------------------------------------------------") + N := 2 + fmt.Println("固定的参数, N =", N) + r1 := df.Col("x").Diff(N).Values() + fmt.Println("序列化结果:", r1) + fmt.Println("------------------------------------------------------------") + d2 := []float64{1, 2, 3, 4, 3, 3, 2, 1, Nil2Float64, Nil2Float64, Nil2Float64, Nil2Float64} + s2 := NewSeries(SERIES_TYPE_FLOAT, "x", d2) + fmt.Printf("序列化参数: %+v\n", s2.Values()) + r2 := df.Col("x").Diff(s2).Values() + fmt.Println("序列化结果:", r2) +} diff --git a/series.go b/series.go index 618cf8f..eadad4a 100644 --- a/series.go +++ b/series.go @@ -77,6 +77,8 @@ type Series interface { Append(values ...interface{}) // Apply 接受一个回调函数 Apply(f func(idx int, v any)) + // Diff 元素的第一个离散差 + Diff(param any) (s Series) } // NewSeries 指定类型创建序列 -- Gitee