Dealing with Missing Values ​​in Deedle Time Series in F # (3)

This is another follow-up to Working with Missing Values ​​in Deedle Time Series in F # (2)

I wrote this function for the map2 series, returning a missing value when any input is missing.

let map2series (f:'T1->'T2->'R)(series1:Series<'K,'T1 opt>)(series2:Series<'K,'T2 opt>):Series<'K,'R opt>=
     let S = series1.Zip(series2,JoinKind.Outer) //Series<'K,('T1 opt opt * 'T2 opt opt)>

     S |> Series.mapValues (fun (a,b) -> match (a,b) with 
                                          | (OptionalValue.Present(a'), OptionalValue.Present(b')) -> OptionalValue.map2 f a' b'
                                          | _ -> OptionalValue.Missing)

      

since it Series<'K,'T opt>

naturally appears in Deedle after using the methods .Zip

or .Join

.

However, as seen earlier, it Series<'K,'V>

already supports missing values, so I would like to rewrite the above function that will basically do the same, except that it accepts Series<'K,'V>

as inputs

let map2series1 (f:'T1->'T2->'R)(series1:Series<'K,'T1>)(series2:Series<'K,'T2>):Series<'K,'R>=
     let S = series1.Zip(series2,JoinKind.Outer) //Series<'K,('T1 opt * 'T2 opt)>
     S |> Series.mapValues (fun (a,b) -> match (a,b) with 
                                          | (OptionalValue.Present(a'), OptionalValue.Present(b')) -> f a' b'
                                          | _ -> None)

      

However this doesn't work, I don't have the correct syntax in the second case of the concatenation when one value is missing ...

basically instead of the latter None

, I need to assign a value that matches <missing>

, but I can't find it.

I also looked at something like Option.bind OptionalValue.asOption OptionalValue.Missing

but cannot find the correct expression

+3


source to share


1 answer


You can do it like this:



let map2 f =
    function
    | OptionalValue.Present(a'), OptionalValue.Present(b') -> Some (f a' b')
    | _ -> None

let map2series f series1 series2 =
     series2
     |> Series.zip series1
     |> Series.mapAll(fun _ -> Option.bind(map2 f)) 

      

+1


source







All Articles