Search code examples
f#deedle

Deedle series index out of range


Hi I'm using the following code:

let timeser = ser |> Series.sampleTimeInto(TimeSpan(0,5,0)) Direction.Backward Series.lastValue

However often get the following error; System.IndexOutOfRangeException with the Additional information: Index was outside the bounds of the array.

Does anyone know how to solve this error.


Solution

  • It is a bit difficult to guess what is the problem, because we cannot run your code. But the most obvious reason for the exception is that Series.lastValue fails because one of your chunks has no data in it.

    Let's say that you have a series with values for just 2 days:

    let ser = series [ DateTime(2000, 1, 1) => 1.0; DateTime(2000, 1, 2) => 2.0 ]
    

    If you try to sample it into 12 hour chunks using Series.lastValue, then this fails (because only two of the chunks you get actually contain some values):

    // This shows you that some of the chunks are empty
    ser |> Series.sampleTime (TimeSpan(12,0,0)) Direction.Backward 
    // This will fail
    ser |> Series.sampleTimeInto(TimeSpan(12,0,0)) Direction.Backward Series.lastValue
    

    You can handle this in various ways, but the easiest one would be to return a missing value for chunks with no data:

    ser |> Series.sampleTimeInto(TimeSpan(12,0,0)) Direction.Backward (fun s -> 
      if s.IsEmpty then nan else Series.lastValue s)