public void FrameTableDictionariesRoundtrip() { FrameTable frame = new FrameTable(); frame.AddColumn <string>("name"); frame.AddColumn <double>("height"); frame.AddColumn <bool?>("male"); frame.AddRow("a", 5.0, false); frame.AddRow("b", 6.0, true); frame.AddRow("c", 5.5, null); List <Dictionary <string, object> > dictionaries = frame.ToDictionaries().ToList(); Assert.IsTrue(dictionaries.Count == frame.Rows.Count); Assert.IsTrue(dictionaries[0].Count == frame.Columns.Count); FrameTable frame2 = FrameTable.FromDictionaries(dictionaries); Assert.IsTrue(frame2.Rows.Count == frame.Rows.Count); Assert.IsTrue(frame2.Columns.Count == frame.Columns.Count); Assert.IsTrue(frame2.Columns[0].Name == frame.Columns[0].Name); Assert.IsTrue(frame2.Columns[1].StorageType == frame.Columns[1].StorageType); Assert.IsTrue(frame2.Rows[2]["male"] == frame2.Rows[2]["male"]); }
public static void ImportingData() { FrameTable data; using (TextReader reader = File.OpenText("test.csv")) { data = FrameTable.FromCsv(reader); } Console.WriteLine($"Imported CSV file with {data.Rows.Count} rows."); Console.WriteLine("The names and types of the columns are:"); foreach (FrameColumn column in data.Columns) { Console.WriteLine($" {column.Name} of type {column.StorageType}"); } FrameTable titanic; Uri url = new Uri("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"); WebRequest request = WebRequest.Create(url); using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { titanic = FrameTable.FromCsv(reader); } } Uri jsonUrl = new Uri("https://raw.githubusercontent.com/dcwuser/metanumerics/master/Examples/Data/example.json"); WebClient client = new WebClient(); string input = client.DownloadString(jsonUrl); List <Dictionary <string, object> > output = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(input); FrameTable jsonExample = FrameTable.FromDictionaries(output); // Define the schema. FrameTable table = new FrameTable(); table.AddColumn <int>("Id"); table.AddColumn <string>("Name"); table.AddColumn <string>("Sex"); table.AddColumn <DateTime>("Birthdate"); table.AddColumn <double>("Height"); table.AddColumn <double?>("Weight"); table.AddColumn <bool>("Result"); // Add rows using as arrays of objects. table.AddRow(1, "John", "M", DateTime.Parse("1970-01-02"), 190.0, 75.0, true); table.AddRow(2, "Mary", "F", DateTime.Parse("1980-02-03"), 155.0, null, true); // Add a row using a dictionary. This is more verbose, but very clear. table.AddRow(new Dictionary <string, object>() { { "Id", 3 }, { "Name", null }, { "Sex", "M" }, { "Birthdate", DateTime.Parse("1990-03-04") }, { "Height", 180.0 }, { "Weight", 60.0 }, { "Result", false } }); }