|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.IO;
|
| 5 | +using System.Linq; |
5 | 6 | using System.Text;
|
6 | 7 |
|
7 | 8 | namespace PandasNET.Extensions
|
8 | 9 | {
|
9 | 10 | public static partial class PandasExtensions
|
10 | 11 | {
|
11 |
| - public static DataFrame<T> DataFrame<T>(this Pandas pd, NDArray<T> data, IList<int> index = null, IList<string> columns = null) |
| 12 | + public static DataFrame<TInd,TValue> DataFrame<TInd,TValue>(this Pandas pd, NDArray<TValue>[] data, IList<TInd> index = null, IList<string> columns = null) |
12 | 13 | {
|
13 |
| - var df = new DataFrame<T>(); |
14 |
| - df.Columns.Array(columns); |
15 |
| - df.Data = data.Data; |
16 |
| - df.Shape = data.Shape; |
| 14 | + var df = new DataFrame<TInd,TValue>(); |
| 15 | + |
| 16 | + if (columns == null) |
| 17 | + columns = Enumerable.Range(0,(data.Length-1)).Select(x => x.ToString()).ToArray(); |
| 18 | + |
| 19 | + Type indexType = typeof(TInd); |
| 20 | + |
| 21 | + if (index == null) |
| 22 | + { |
| 23 | + dynamic indexDyn = index; |
| 24 | + switch (indexType.Name) |
| 25 | + { |
| 26 | + case ("Double"): indexDyn = Enumerable.Range(0,data[0].Data.Length).Select(x => (double) x).ToList() ; break; |
| 27 | + case ("Int32"): indexDyn = Enumerable.Range(0,data[0].Data.Length).ToList() ; break; |
| 28 | + } |
| 29 | + index = (List<TInd>) indexDyn; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + for(int idx = 0; idx < columns.Count;idx++) |
| 37 | + df[columns[idx]] = data[idx]; |
| 38 | + |
| 39 | + df.Index = new Index<TInd>(); |
| 40 | + df.Index.Values = new NDArray<TInd>(); |
| 41 | + df.Index.Values.Data = index.ToArray(); |
| 42 | + df.Index.Values.Shape = new Shape(index.Count); |
17 | 43 |
|
18 | 44 | return df;
|
19 | 45 | }
|
| 46 | + public static DataFrame<TInd,TValue> DataFrame<TInd,TValue>(this Pandas pd, NDArray<TValue> data, IList<TInd> index = null, IList<string> columns = null) |
| 47 | + { |
| 48 | + |
| 49 | + var vectors = new NDArray<TValue>[data.Shape.Shapes[1]]; |
| 50 | + |
| 51 | + for (int idx = 0;idx < data.Shape.Shapes[1];idx++) |
| 52 | + { |
| 53 | + vectors[idx] = new NDArray<TValue>(); |
| 54 | + vectors[idx].Data = new TValue[data.Shape.Shapes[0]]; |
| 55 | + for (int jdx = 0; jdx < data.Shape.Shapes[0];jdx++) |
| 56 | + { |
| 57 | + vectors[idx].Data[jdx] = data[jdx,idx]; |
| 58 | + } |
| 59 | + } |
| 60 | + return pd.DataFrame<TInd,TValue>(vectors,index,columns); |
| 61 | + } |
20 | 62 | }
|
21 | 63 | }
|
0 commit comments