Search code examples
f#deedle

How can I use Deedle to get every row from some key and below?


I want to return every value up to and including some key. Whilst I could generate every such key and chuck them all into the Get, I suspect this will inefficiently search for the value of every key.

Inspired by this answer, I have come up with the following

let getAllUpTo key (frame:Frame<'key,'col>) : Frame<'key, 'col> =
    let endRng = frame.RowIndex.Locate key
    let startRng = frame.RowIndex.KeyRange |> fst |> frame.RowIndex.Locate
    let fixedRange = RangeRestriction.Fixed (startRng, endRng)
    frame.GetAddressRange fixedRange

Is there a built in method for doing this efficiently?


Solution

  • If you want to access a sub-range of a data frame with a specified starting/ending key, you can do this using the df.Rows.[ ... ] indexer. Say we have some data indexed by (sorted) dates:

    let s1 = series [
      let rnd = Random()
      for d in 0 .. 365 ->
        DateTime(2020, 1, 1).AddDays(float d) => rnd.Next() ]
    
    let df = frame [ "S1" => s1 ]
    

    To get a part of the data frame starting/ending on a specific date, you can use:

    // Get all rows from 1 June (inclusive)  
    df.Rows.[DateTime(2020, 6, 1) ..]
    
    // Get all rows until 1 June (inclusive)  
    df.Rows.[.. DateTime(2020, 6, 1)]
    

    The API you are using is essentially what this does under the cover - but you are using a very low-level operations that you do not typically need to use in user code.