forked from SciSharp/Pandas.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeriesBase.cs
More file actions
129 lines (117 loc) · 3.69 KB
/
Copy pathSeriesBase.cs
File metadata and controls
129 lines (117 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using NumSharp;
using PandasNet.Impl;
using PandasNet.Iteration;
using System;
using System.Collections.Generic;
using System.Text;
namespace PandasNet
{
public abstract class SeriesBase : PandasObject, IPandasObject, IRowIndexable
{
public IDataIndex Index { get; set; }
public IDataFrame this[Slice s] => throw new NotImplementedException();
/// <summary>
/// 转换为指定的dtype
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="copy"></param>
/// <returns></returns>
public abstract SeriesBase AsType<T>(bool copy = true);
public abstract object this[int index] { get; set; }
public abstract object this[string idx] { get; }
public static NDArray operator +(SeriesBase a, SeriesBase b)
{
if (a.Shape != b.Shape)
{
throw new Exception("相加的Series长度Shape不相等");
}
NDArray nd = new NDArray(typeof(object), a.Shape);
for (var i = 0; i < a.Size; i++)
{
if (a[i] is string || b[i] is string)
{
nd[i] = a[i].ToString() + b[i].ToString();
}
else
{
try
{
nd[i] = Convert.ToDecimal(a[i]) + Convert.ToDecimal(b[i]);
}
catch (InvalidCastException)
{
nd[i] = null;
}
}
}
return nd;
}
public static NDArray operator -(SeriesBase a, SeriesBase b)
{
if (a.Shape != b.Shape)
{
throw new Exception("相加的Series长度Shape不相等");
}
NDArray nd = new NDArray(typeof(object), a.Shape);
for (var i = 0; i < a.Size; i++)
{
if (a[i] is string || b[i] is string)
{
nd[i] = null;
}
else
{
try
{
nd[i] = Convert.ToDecimal(a[i]) - Convert.ToDecimal(b[i]);
}
catch (InvalidCastException)
{
nd[i] = null;
}
}
}
return nd;
}
public static NDArray operator *(SeriesBase a, SeriesBase b)
{
if (a.Shape != b.Shape)
{
throw new Exception("相加的Series长度Shape不相等");
}
NDArray nd = new NDArray(typeof(object), a.Shape);
for (var i = 0; i < a.Size; i++)
{
try
{
nd[i] = Convert.ToDecimal(a[i]) * Convert.ToDecimal(b[i]);
}
catch (InvalidCastException)
{
nd[i] = null;
}
}
return nd;
}
public static NDArray operator /(SeriesBase a, SeriesBase b)
{
if (a.Shape != b.Shape)
{
throw new Exception("相加的Series长度Shape不相等");
}
NDArray nd = new NDArray(typeof(object), a.Shape);
for (var i = 0; i < a.Size; i++)
{
try
{
nd[i] = Convert.ToDecimal(a[i]) / Convert.ToDecimal(b[i]);
}
catch (DivideByZeroException)
{
nd[i] = null;
}
}
return nd;
}
}
}