The OpenD Programming Language

csvToAlgebraicMatrix

@trusted pure
Slice!(CsvAlgebraic*, 2)
csvToAlgebraicMatrix
(
return scope string text
,
char separator = ','
,
char quote = '"'
,
scope const CsvProxy.Conversion[] conversions = CsvProxy.init.conversions
,
char comment = '\0'
,
ubyte skipRows = 0
,
bool fill = true
,
bool skipEmptyLines = true
,
bool parseNumbers = true
,
bool parseTimestamps = true
,
CsvAlgebraic delegate
(
return scope const(char)[] unquotedString
,
CsvAlgebraic scalar
,
bool quoted
,)
@safe pure
conversionFinalizer = null
)

Return Value

Type: Slice!(CsvAlgebraic*, 2)

$(NDSLICEREF slice, Slice)!(string*, 2).

Examples

    import mir.csv;
    import mir.ion.conv: serde; // to convert CsvProxy to D types
    import mir.serde: serdeKeys, serdeIgnoreUnexpectedKeys, serdeOptional;
    // mir.date and std.datetime are supported as well
    import mir.timestamp: Timestamp;//mir-algorithm package
    import mir.test: should;

    auto text =
`Date,Open,High,Low,Close,Volume
2021-01-21 09:30:00,133.8,134.43,133.59,134.0,9166695,ignoreNoHeader
2021-01-21 09:35:00,134.25,135.0,134.19,134.5`;// fill the Volume with '0'

    // If you don't have a header,
    // `mir.functional.Tuple` instead of MyDataFrame.
    @serdeIgnoreUnexpectedKeys //ignore all other columns
    static struct MyDataFrame
    {
        // Few keys are allowed
        @serdeKeys(`Date`, `date`, `timestamp`)
        Timestamp[] timestamp;

        @serdeKeys(`Open`)  double[]    open;
        @serdeKeys(`High`)  double[]    high;
        @serdeKeys(`Low`)   double[]    low;
        @serdeKeys(`Close`) double[]    close;

        @serdeOptional // if we don't have Volume
        @serdeKeys(`Volume`)
        long[]volume;
    }

    MyDataFrame testValue = {
        timestamp:  [`2021-01-21 09:30:00`.Timestamp, `2021-01-21 09:35:00`.Timestamp],
        volume:     [9166695, 0],
        open:       [133.8,  134.25],
        high:       [134.43, 135],
        low:        [133.59, 134.19],
        close:      [134.0,  134.5],
    };

    auto table = text         // fill the missing and empty fields with '0'
        .csvToAlgebraicMatrix(',', '"', [CsvProxy.Conversion("", 0.CsvAlgebraic)])
        .matrixAsDataFrame;

    table["Volume"][0].should == 9166695;
    table["Volume"][1].should == 0;

    table.serde!MyDataFrame.should == testValue;

See Also

Meta