Search code examples
c#deedle

Convert a Deedle Dataframe into a C# List of Custom Class


I receive data from an external API as a Deedle dataframe. I need to take this data and convert it to a List of a custom class to be inserted into a database via Entity Framework.

Would anyone be able to point me in the right direction? I've not used Deedle before and am having trouble finding the best way to extract data.

The custom object I need to populate looks like the below:

public class FrameData
{
    public string SecurityId { get; set; }
    public string FieldName { get; set; }
    public DateTime Date { set; get; }
    public decimal value { get; set; }
}

Thanks, Nick


Solution

  • There are many ways to get data from a Deedle frame. Does Entity Framework allow you to use interfaces? If so, then there is a nice function GetRowsAs which lets you do this:

    // Given a simple Person interface
    public interface Person {
      int Age { get; }
      string Name{ get; }
    }
    
    // And a sample data frame with some data
    Frame<int, string> df = Frame.FromValues(new[] {
        Tuple.Create(1, "Name", (object) "One"),
        Tuple.Create(2, "Name", (object) "Two"),
        Tuple.Create(1, "Age", (object) 42),
        Tuple.Create(2, "Age", (object) 21)
      });
    
    // You can get an array of rows using 
    var rows = df.GetRowsAs<Person>();
    

    If Entity Framework cannot handle interfaces, then this method sadly won't work. In that case, you'll need something like:

    var rows = 
        df.Rows.Select(row =>
          new Person { Name = row.Value.GetAs<string>("Name"), 
                       Age = row.Value.GetAs<int>("Age"))).Observations;