From ff5ff91c2ce522176f96ae3e37746dd40bf5af88 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Wed, 1 Feb 2023 18:02:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_bool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/type_bool.go b/type_bool.go index 44803dd..b0a546f 100644 --- a/type_bool.go +++ b/type_bool.go @@ -85,7 +85,7 @@ func AnyToBool(v any) bool { } } -// ParseFloat 字符串转float64 +// ParseBool 字符串转bool // 任意组合的nan字符串都会被解析成NaN func ParseBool(s string, v any) bool { f, err := strconv.ParseBool(s) -- Gitee From e6d7e8cd354c01958bf624301a34df47d90a12e0 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Wed, 1 Feb 2023 21:00:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?#I6CCA6=20series=E6=B3=9B=E5=9E=8B=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=AE=9E=E7=8E=B0fillna=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic.go | 18 ++++++++++++++++++ generic_test.go | 1 + series.go | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/generic.go b/generic.go index 13c2534..a363d4f 100644 --- a/generic.go +++ b/generic.go @@ -354,3 +354,21 @@ func (self *NDFrame) StdDev() float64 { stdDev := stat.StdDev(values, nil) return stdDev } + +func (self *NDFrame) FillNa(v any, inplace bool) { + values := self.Values() + switch rows := values.(type) { + case []string: + for idx, iv := range rows { + if StringIsNaN(iv) && inplace { + rows[idx] = AnyToString(v) + } + } + case []float64: + for idx, iv := range rows { + if IsNaN(iv) && inplace { + rows[idx] = AnyToFloat64(v) + } + } + } +} diff --git a/generic_test.go b/generic_test.go index 5c5393f..5672e6b 100644 --- a/generic_test.go +++ b/generic_test.go @@ -74,6 +74,7 @@ func TestNDFrameNew(t *testing.T) { // string d2 := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "nan", "12"} nd2 := NewNDFrame[string]("x", d2...) + nd2.FillNa(0, true) fmt.Println(nd2) fmt.Println(nd2.Records()) fmt.Println(nd2.Empty()) diff --git a/series.go b/series.go index 279efa8..3bfa2eb 100644 --- a/series.go +++ b/series.go @@ -56,6 +56,8 @@ type Series interface { Mean() float64 // StdDev calculates the standard deviation of a series StdDev() float64 + // FillNa Fill NA/NaN values using the specified method. + FillNa(v any, inplace bool) } // NewSeries 指定类型创建序列 @@ -193,3 +195,25 @@ func Shift[T GenericType](s *Series, periods int, cbNan func() T) Series { _ = naVals return d } + +// FillNa 填充NaN的元素为v +// inplace为真是修改series元素的值 +// 如果v和Values()返回值的slice类型不一致就会panic +func FillNa[T GenericType](s *NDFrame, v T, inplace bool) *NDFrame { + values := s.Values() + switch rows := values.(type) { + case []string: + for idx, iv := range rows { + if StringIsNaN(iv) && inplace { + rows[idx] = AnyToString(v) + } + } + case []float64: + for idx, iv := range rows { + if IsNaN(iv) && inplace { + rows[idx] = AnyToFloat64(v) + } + } + } + return s +} -- Gitee From 5b21b19043156e35dfc6045e8c7b24da9073bc26 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Wed, 1 Feb 2023 21:10:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A1=A5=E5=85=A8bool/string/int64/float64?= =?UTF-8?q?=E7=9A=84series?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic.go | 6 ++++++ series_bool.go | 13 +++++++++++++ series_float64.go | 12 ++++++++++++ series_int64.go | 13 +++++++++++++ series_xstring.go | 12 ++++++++++++ 5 files changed, 56 insertions(+) diff --git a/generic.go b/generic.go index a363d4f..381bc4c 100644 --- a/generic.go +++ b/generic.go @@ -364,6 +364,12 @@ func (self *NDFrame) FillNa(v any, inplace bool) { rows[idx] = AnyToString(v) } } + case []int64: + for idx, iv := range rows { + if IsNaN(float64(iv)) && inplace { + rows[idx] = AnyToInt64(v) + } + } case []float64: for idx, iv := range rows { if IsNaN(iv) && inplace { diff --git a/series_bool.go b/series_bool.go index 89cd783..aae892a 100644 --- a/series_bool.go +++ b/series_bool.go @@ -159,3 +159,16 @@ func (self *SeriesBool) StdDev() float64 { //TODO implement me panic("implement me") } + +// FillNa bool类型不可能在导入series还是NaN +func (self *SeriesBool) FillNa(v any, inplace bool) { + //values := self.Values() + //switch rows := values.(type) { + //case []bool: + // for idx, iv := range rows { + // if IsNaN(float64(iv)) && inplace { + // rows[idx] = AnyToFloat64(v) + // } + // } + //} +} diff --git a/series_float64.go b/series_float64.go index 90c18ec..4f2a4fa 100644 --- a/series_float64.go +++ b/series_float64.go @@ -221,3 +221,15 @@ func (self *SeriesFloat64) StdDev() float64 { stdDev := stat.StdDev(values, nil) return stdDev } + +func (self *SeriesFloat64) FillNa(v any, inplace bool) { + values := self.Values() + switch rows := values.(type) { + case []float64: + for idx, iv := range rows { + if IsNaN(iv) && inplace { + rows[idx] = AnyToFloat64(v) + } + } + } +} diff --git a/series_int64.go b/series_int64.go index 2f11790..5880209 100644 --- a/series_int64.go +++ b/series_int64.go @@ -178,3 +178,16 @@ func (self *SeriesInt64) StdDev() float64 { stdDev := stat.StdDev(d, nil) return stdDev } + +// FillNa int64没有NaN +func (self *SeriesInt64) FillNa(v any, inplace bool) { + values := self.Values() + switch rows := values.(type) { + case []int64: + for idx, iv := range rows { + if IsNaN(float64(iv)) && inplace { + rows[idx] = AnyToInt64(v) + } + } + } +} diff --git a/series_xstring.go b/series_xstring.go index c149cef..063c4c2 100644 --- a/series_xstring.go +++ b/series_xstring.go @@ -164,3 +164,15 @@ func (self *SeriesString) StdDev() float64 { //TODO implement me panic("implement me") } + +func (self *SeriesString) FillNa(v any, inplace bool) { + values := self.Values() + switch rows := values.(type) { + case []string: + for idx, iv := range rows { + if StringIsNaN(iv) && inplace { + rows[idx] = AnyToString(v) + } + } + } +} -- Gitee