Search code examples
f#missing-datadeedle

Is there a better way to find the first key of a missing value in a Deedle Series?


I need to find the key to the first missing value in a Deedle Series. I was able to write the function below, which seems to work. I wonder if Deedle has a better method, and more generally a fast function that returns all the keys of missing (or non-missing) values.

let firstMissing (s: Series<'a,_>) = 
let e = (s.Keys).GetEnumerator()

let rec loop () =
    if e.MoveNext() then
        if s |> Series.has e.Current then loop ()
        else e.Current |> Some
    else None

loop ()

let s2 = Series.ofOptionalObservations [ 1 => Some(1.0); 2 => None ]
// val it : Series<int,float> =
  series [ 1 => 1; 2 => <missing>]
firstMissing s2
val it : int option = Some 2

let s4 = Series.ofValues [1..4]
firstMissing s4
val it : int option = None

Solution

  • I don't think there is any principally different option than scanning the keys until you find one that does not have a value, but you can do it more nicely using Series.observationsAll which returns a sequence of keys, paired with optional values:

    s2 |> Series.observationsAll |> Seq.tryPick (function (k, None) -> Some k | _ -> None)
    

    This returns None if all keys have values and Some k with the first key without value otherwise.