Search code examples
c#deedle

How to add keyvalue pair to deedle Series?


I have Series<string,object> and I need to create new Series<string,object> from it by one adding key-value pair. Is it possible in any nice way but not decomposing original Series in loop and creating new via SeriesBuilder? I couldn't find solution searching in poor Deedle C# documentation.


Solution

  • Appending one key-value pair to a series is not the most typical thing to do with Deedle. The reason is that if you're writing code in a functional way, you'll typically end up using higher-level operations like maps and filters. You can certainly add one key-value pair, but if you were doing that in a loop, that would be inefficient and you should use a more high-level way of expressing your logic instead.

    If you don't need to do this in a loop and really just want to append one key-value pair, I would create a new series containing the new element and use the Merge operation:

    // Create: 0 -> A, 1 -> B, 2 -> C
    var s1 = new[] { "A", "B", "C" }.ToOrdinalSeries();
    
    // Append: 5 -> Z
    var s2 = s1.Merge(new[] { KeyValue.Create(5, "Z") }.ToSeries());
    
    // Print the new series
    s2.Print();
    

    The C# documentation for Deedle is definitely lacking behind the F# one, but it is all open-source and available in pretty simple Markdown format on GitHub, so you are welcome to contribute what you learned and found missing!