series

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2024 License: MulanPSL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArithmeticOperator

type ArithmeticOperator int

ArithmeticOperator 算术运算

const (
	// Addition 加
	Addition ArithmeticOperator = iota
	// Subtraction 减
	Subtraction
	// Multiplication 乘
	Multiplication
	// Division 除
	Division
	// Remainder 求于
	Remainder
)

type Element

type Element interface {
	// Set 设置值
	Set(any)
	// Records 返回字符串
	Records() string
	// Int 返回整数
	Int() int
	// Float 返回浮点数
	Float() float64
	// Bool 返回布尔值
	Bool() bool
	// Value 任一类型
	Value() any
	// contains filtered or unexported methods
}

func NewElements

func NewElements(t Type, l int) []Element

NewElements 生成 Elements 接口的对象

type RelationalOperator

type RelationalOperator int

RelationalOperator 关系运算

const (
	// Equal 相等
	Equal RelationalOperator = iota
	// NotEqual 不相等
	NotEqual
	// LessThan 小于
	LessThan
	// LessOrEqual 小于等于
	LessOrEqual
	// GreaterThan 大于
	GreaterThan
	// GreaterOrEqual 大于等于
	GreaterOrEqual
	// Contains 包含
	Contains
	// StartsWith 以...开始
	StartsWith
	// EndsWith 以...结束
	EndsWith
	// In 在...列表里
	In
	// NotIn 不在...列表里
	NotIn
)

type Series

type Series struct {
	Name string
	// contains filtered or unexported fields
}

func LoadRecords

func LoadRecords(values []string, t Type, name string) *Series

LoadRecords 用字符串切片创建指定类型数据列

values: 数据切片
dType: 数据类型,可选String、Int、float64、Bool
name: 数据列名称
Example
s1 := LoadRecords([]string{"1", "2", "test", "2", "4", "6"}, Int, "number1")
fmt.Println(s1)
Output:

	字段名:number1
数 据:[1 2 NaN 2 4 6]
索 引:[0 1 2 3 4 5]
类 型:int

func NewSeries

func NewSeries[S interface{ ~[]E }, E int | float64 | string | bool](values S, dType Type, name string) (*Series, error)

NewSeries 创建数据列

values: 数据切片
dType: 数据类型,可选String、Int、float64、Bool
name: 数据列名称
Example
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
fmt.Println(s1)
Output:

字段名:number1
数 据:[1 2 test 2 4 6]
索 引:[0 1 2 3 4 5]
类 型:string
Example (A)
s1, _ := NewSeries([]int{1, 2, 3, 4, 6}, Int, "number2")
fmt.Println(s1)
Output:

字段名:number2
数 据:[1 2 3 4 6]
索 引:[0 1 2 3 4]
类 型:int
Example (Err)
s1, err := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, Float, "number3")
fmt.Println(s1, err)
Output:

<nil> 输入切片与指定数据类型不匹配

func (*Series) Any

func (s *Series) Any() []any

func (*Series) Append

func (s *Series) Append(values interface{}) error

Append 向数据集后添加元素,可以为单个元素或元素切片,最好保证数据类型一致

values:Series、int []int、string []string ...
Example
s1, _ := NewSeries(
	[]string{"1", "2", "test", "2", "4", "6"},
	String,
	"number1",
)
_ = s1.Append([]string{"1", "2", "test", "2", "4", "6"})
fmt.Println(s1)
Output:

字段名:number1
数 据:[1 2 test 2 4 6 1 2 test 2 4 6]
索 引:[0 1 2 3 4 5 6 7 8 9 10 11]
类 型:string
Example (A)
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
_ = s1.Append(1)
fmt.Println(s1)
Output:

字段名:number1
数 据:[1 2 test 2 4 6 1]
索 引:[0 1 2 3 4 5 6]
类 型:string

func (*Series) Arithmetic

func (s *Series) Arithmetic(operator ArithmeticOperator, x Series) (*Series, error)

Arithmetic 算术运算

Example (A2)
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
s2, _ := NewSeries([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, String, "name2")
s, _ := s2.Arithmetic(Addition, *s1)
fmt.Println(s)
Output:

字段名:name2
数 据:[a10 b1 c5 d3 e7 f5 g1 h36 i5]
索 引:[]
类 型:string
Example (Add)
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
s2, _ := NewSeries([]float64{10.1, 1.0, 5.5, 3.3, 7.0, 5.0, 0, 36.0, 5.0}, Float, "number1")
s, _ := s1.Arithmetic(Addition, *s2)
fmt.Println(s)
Output:

字段名:number1
数 据:[20.1 2 10.5 6.3 14 10 1 72 10]
索 引:[]
类 型:float64
Example (Div)
s1, _ := NewSeries([]int{20, 10, 5, 9, 25}, Int, "number1")
s2, _ := NewSeries([]float64{2.5, 2, 5, 0, 12.5}, Float, "number1")
s, _ := s1.Arithmetic(Division, *s2)
fmt.Println(s)
Output:

字段名:number1
数 据:[8 5 1 NaN 2]
索 引:[]
类 型:float64
Example (Mod)
s1, _ := NewSeries([]int{10, 1, 5, 3, 7}, Int, "number1")
s2, _ := NewSeries([]float64{3, 1.0, 3, 0.5, 2}, Float, "number1")
s, _ := s1.Arithmetic(Remainder, *s2)
fmt.Println(s)
Output:

字段名:number1
数 据:[1 0 2 0 1]
索 引:[]
类 型:float64
Example (Mul)
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
s2, _ := NewSeries([]float64{10.1, 1.0, 5.5, 3.3, 7.0, 5.0, 0, 36.0, 5.0}, Float, "number1")
s, _ := s1.Arithmetic(Multiplication, *s2)
fmt.Println(s)
Output:

字段名:number1
数 据:[101 1 27.5 9.9 49 25 0 1296 25]
索 引:[]
类 型:float64
Example (Sub)
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
s2, _ := NewSeries([]float64{10.1, 1.0, 5.5, 3.3, 7.0, 5.0, 0, 36.0, 5.0}, Float, "number1")
s, _ := s1.Arithmetic(Subtraction, *s2)
fmt.Println(s)
Output:

字段名:number1
数 据:[-0.1 0 -0.5 -0.3 0 0 1 0 0]
索 引:[]
类 型:float64

func (*Series) Bool

func (s *Series) Bool() []bool

Bool 将数据集中的元素作为浮布尔值返回

func (*Series) Concat

func (s *Series) Concat(x Series) error

Concat 将新数据集加原数据集后,如果类型不同,以原数据集为准

Example
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
s2, _ := NewSeries([]int{1, 2, 3, 4, 6}, Int, "number2")
_ = s1.Concat(*s2)
fmt.Println(s1)
Output:

两个数据类型不同,正在尝试转换... done
字段名:number1
数 据:[1 2 test 2 4 6 1 2 3 4 6]
索 引:[0 1 2 3 4 5 6 7 8 9 10]
类 型:string

func (*Series) Copy

func (s *Series) Copy() *Series

Copy 复制

Example
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
s2 := s1.Copy()
fmt.Println(&s1 == &s2)
Output:

false

func (*Series) Drop

func (s *Series) Drop(indexes ...int) *Series

Drop 删除指定索引的元素

Example
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
s1 = s1.Drop(3, 6, 4)
fmt.Println(s1)
Output:

字段名:number1
数 据:[10 1 5 5 36 5]
索 引:[0 1 2 5 7 8]
类 型:int

func (*Series) Element

func (s *Series) Element(i int) Element

Element 指定索引元素

func (*Series) Elements

func (s *Series) Elements() []Element

Elements 返回数据集元素对象切片

func (*Series) Filter

func (s *Series) Filter(operator RelationalOperator, values any) (*Series, error)

Filter 过滤数据集

Example
s1, _ := NewSeries([]string{"aee", "2", "test", "2", "any", "6"}, String, "number1")
ns, _ := s1.Filter(Equal, "2")
fmt.Println(ns)
fmt.Println("-----------------------------")
ns1, _ := s1.Filter(NotEqual, "2")
fmt.Println(ns1)
fmt.Println("-----------------------------")
ns2, _ := s1.Filter(Contains, "e")
fmt.Println(ns2)
fmt.Println("-----------------------------")
ns3, _ := s1.Filter(StartsWith, "a")
fmt.Println(ns3)
fmt.Println("-----------------------------")
ns4, _ := s1.Filter(EndsWith, "e")
fmt.Println(ns4)
Output:

字段名:number1
数 据:[2 2]
索 引:[1 3]
类 型:string

-----------------------------
字段名:number1
数 据:[aee test any 6]
索 引:[0 2 4 5]
类 型:string

-----------------------------
字段名:number1
数 据:[aee test]
索 引:[0 2]
类 型:string

-----------------------------
字段名:number1
数 据:[aee any]
索 引:[0 4]
类 型:string

-----------------------------
字段名:number1
数 据:[aee]
索 引:[0]
类 型:string
Example (A)
s1, _ := NewSeries([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 100, 200, 300, 3, 8, 4}, Int, "number1")
ns, _ := s1.Filter(Equal, 3)
fmt.Println(ns)
fmt.Println("-----------------------------")
ns1, _ := s1.Filter(LessThan, 6)
fmt.Println(ns1)
fmt.Println("-----------------------------")
ns2, _ := s1.Filter(LessOrEqual, 8)
fmt.Println(ns2)
fmt.Println("-----------------------------")
ns3, _ := s1.Filter(GreaterOrEqual, 10)
fmt.Println(ns3)
fmt.Println("-----------------------------")
ns4, _ := s1.Filter(In, []int{3, 8, 200})
fmt.Println(ns4)
Output:

字段名:number1
数 据:[3 3 3]
索 引:[2 10 14]
类 型:int

-----------------------------
字段名:number1
数 据:[1 2 3 4 5 3 3 4]
索 引:[0 1 2 3 4 10 14 16]
类 型:int

-----------------------------
字段名:number1
数 据:[1 2 3 4 5 6 7 8 3 3 8 4]
索 引:[0 1 2 3 4 5 6 7 10 14 15 16]
类 型:int

-----------------------------
字段名:number1
数 据:[10 100 200 300]
索 引:[9 11 12 13]
类 型:int

-----------------------------
字段名:number1
数 据:[3 8 3 200 3 8]
索 引:[2 7 10 12 14 15]
类 型:int

func (*Series) Float

func (s *Series) Float() []float64

Float 将数据集中的元素作为浮点数返回,如果数据转换失败自动设为 math.NaN()

func (*Series) Format

func (s *Series) Format(f func(index int, elem Element) Element)

Format 批量处理数据集

index: 元素索引
elem: 元素对象
Example
s1, _ := NewSeries([]int{1, 2, 3, 4, 6}, Int, "number1")
// value + index
s1.Format(func(index int, elem Element) Element {
	e := elem.Int()
	elem.Set(e + index)
	return elem
})

fmt.Println(s1)
Output:

字段名:number1
数 据:[1 3 5 7 10]
索 引:[0 1 2 3 4]
类 型:int
Example (A)
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number2")
s1.Format(func(index int, elem Element) Element {
	e := elem.Records()
	elem.Set(e + "ioc")
	return elem
})

fmt.Println(s1)
Output:

字段名:number2
数 据:[1ioc 2ioc testioc 2ioc 4ioc 6ioc]
索 引:[0 1 2 3 4 5]
类 型:string

func (*Series) HasNaN

func (s *Series) HasNaN() bool

HasNaN 判断是否存在空值

func (*Series) Indexes

func (s *Series) Indexes() []int

Indexes 返回索引切片

func (*Series) InitIndex

func (s *Series) InitIndex()

InitIndex 重置索引

func (*Series) Int

func (s *Series) Int() []int

Int 将数据集中的元素作为整数返回,如果数据转换失败自动设为 math.MinInt()

func (*Series) Len

func (s *Series) Len() int

Len 返回数据集大小

func (*Series) Records

func (s *Series) Records() []string

Records 将数据集中的元素作为字符串返回

func (*Series) SetType

func (s *Series) SetType(t Type) error

SetType 改变数据集类型

Example
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
fmt.Println(s1)
_ = s1.SetType(Int)
fmt.Println(s1)
Output:

	字段名:number1
数 据:[1 2 test 2 4 6]
索引:[0 1 2 3 4 5]
类 型:string

test 不能转换为 Int,已置为无限小
字段名:number1
数 据:[1 2 NaN 2 4 6]
索 引:[0 1 2 3 4 5]
类 型:int

func (*Series) SortIndex

func (s *Series) SortIndex(reverse bool) []int

SortIndex 生成升序、降序索引变化

Example
s1, _ := NewSeries([]int{10, 1, 5, 3, 7, 5, 1, 36, 5}, Int, "number1")
fmt.Println(s1)
fmt.Println(s1.SortIndex(false))
fmt.Println(s1.SortIndex(true))
Output:

	字段名:number1
数 据:[10 1 5 3 7 5 1 36 5]
索 引:[0 1 2 3 4 5 6 7 8]
类 型:int

[1 6 3 2 5 8 4 0 7]
[7 0 4 2 5 8 3 1 6]

func (*Series) String

func (s *Series) String() string

自定义输出

func (*Series) SubSet

func (s *Series) SubSet(indexes ...int) (*Series, error)

SubSet 保留对应索引的元素

Example
s1, _ := NewSeries([]string{"1", "2", "test", "2", "4", "6"}, String, "number1")
ns, _ := s1.SubSet(1, 2, 3)
fmt.Println(ns)
Output:

	字段名:number1
数 据:[2 test 2]
索 引:[1 2 3]
类 型:string

func (*Series) Type

func (s *Series) Type() string

Type 返回类型

type Type

type Type string
const (
	String Type = "string"
	Int    Type = "int"
	Float  Type = "float64"
	Bool   Type = "bool"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL