Esempio n. 1
0
 public void DataFrame()
 {
     var np    = new NumPy <int>();
     var pd    = new Pandas();
     var array = np.random.randint(low: 0, high: 10, size: new Shape(5, 5));
     var df    = pd.DataFrame(array, columns: new string[] { "a", "b", "c", "d", "e" });
 }
Esempio n. 2
0
        public void SetColumn_Test()
        {
            var dict = new Dictionary <string, NDArray>
            {
                { "one", np.arange(1000) },
                { "two", np.arange(1001, 2001) }
            };
            var pd  = new Pandas();
            var df1 = pd.DataFrame <string>(dict);

            df1["three"] = new Series(np.arange(2001, 3001));
            Assert.Equal(3000, df1.Size);
            df1.SingleColumn("four", 1);
            Assert.Equal(1, df1["four"][500]);
            Assert.Equal(4000, df1.Size);

            df1.SingleColumn(1, 1);
            Assert.Equal(1, df1["two"][500]);
            df1.Column("five", np.arange(3001, 4001));
            Assert.Equal(3001, df1["five"][0]);

            df1.SingleColumn(4, 1);
            Assert.Equal(1, df1["five"][0]);

            Assert.Equal(3000, df1["three"][999]);
            df1["three"] = new Series(np.arange(1000));
            Assert.Equal(999, df1["three"][999]);
        }
Esempio n. 3
0
        private void CreateSliceModel()
        {
            int     row   = 10;
            int     col   = 10;
            var     nd    = np.random.randn(row, col);
            NDArray array = nd;
            //array.reshape(5, 4);
            var           pd      = new Pandas();
            List <string> indexs  = new List <string>();
            List <string> columns = new List <string>();

            CreateRowAndCol(row, col, ref indexs, ref columns);
            IDataFrame df1 = pd.DataFrame(array, indexs, columns, typeof(object));

            _dataFrame = df1;
        }
Esempio n. 4
0
        public void WriteCsvQuoted_ToFile_Test()
        {
            var filepath    = "write_quoted_test.csv";
            var array       = np.arange(100).reshape(20, 5);
            var columnNames = new string[] { "first", "second", "third",
                                             "fourth", "fifth" };
            var        pd  = new Pandas();
            IDataFrame df1 = pd.DataFrame(array, null, columnNames, typeof(object));

            df1.to_csv(filepath, quoting: 1);
            using (var fr = File.OpenText(filepath))
            {
                Assert.Equal(string.Join(',', columnNames), fr.ReadLine());
                for (var i = 0; i < array.shape[0]; i++)
                {
                    Assert.Equal('"' + string.Join("\",\"", array[i].Data <int>()) + '"', fr.ReadLine());
                }
            }
        }
Esempio n. 5
0
        public void Create_Group_Test()
        {
            var dict = new Dictionary <string, NDArray>
            {
                { "col1", np.arange(5) },
                { "col2", np.arange(5) },
                { "col3", np.arange(5) },
                { "col4", np.arange(5) },
                { "col5", np.arange(5) },
            };

            var pd  = new Pandas();
            var df1 = pd.DataFrame <string>(dict, new string[] {
                "row1", "row2", "row3", "row4", "row5"
            });
            var gro   = df1.groupby("col2");
            var group = gro.Groups;
            var a     = gro.Groups[1];

            Assert.Equal("row2", a.Values[0]);
        }
Esempio n. 6
0
        public void WriteCsvFormated_ToFile_Test()
        {
            var filepath    = "write_quoted_test.csv";
            var array       = np.arange(0, 50, 0.5).reshape(20, 5);
            var columnNames = new string[] { "first", "second", "third",
                                             "fourth", "fifth" };
            var        floatFormat = "E03";
            var        pd          = new Pandas();
            IDataFrame df1         = pd.DataFrame(array, null, columnNames, typeof(object));

            df1.to_csv(filepath, float_format: floatFormat);
            using (var fr = File.OpenText(filepath))
            {
                Assert.Equal(string.Join(',', columnNames), fr.ReadLine());
                for (var i = 0; i < array.shape[0]; i++)
                {
                    var formattedData = array[i].Data <double>().Select(
                        x => x.ToString(floatFormat));
                    Assert.Equal(string.Join(",", formattedData), fr.ReadLine());
                }
            }
        }
Esempio n. 7
0
        private static IDataFrame CreateDataFrame(IDataFrame df, List <int> indexs)
        {
            var value    = df.Values.Array;
            int colSize  = df.Columns.Size;
            int rowSize  = df.Index.Size;
            var cols     = df.Columns.Values.Array;
            var rows     = df.Index.Values.Array;
            var rowArray = Array.CreateInstance(df.Index.DType, rows.Length);
            int index    = 0;

            foreach (var ind in indexs)
            {
                rowArray.SetValue(rows.GetValue(ind), index);
                index++;
            }
            object[] objs = new object[df.Size];
            int      ins  = 0;

            for (int c = 0; c < colSize; c++)
            {
                for (int r = 0; r < rowSize; r++)
                {
                    objs[ins] = value.GetValue(indexs[r] + c * rowSize);
                    ins++;
                }
            }
            NDArray nDArray = new NDArray(df.DType, new Shape(rowSize, colSize));

            nDArray.SetData(objs);
            var        pd        = new Pandas();
            IDataFrame dataFrame = pd.DataFrame(nDArray, null, null, df.DType);

            dataFrame.Index.Values.SetData(rowArray);
            dataFrame.Columns.Values.SetData(cols);
            return(dataFrame);
        }