Fagui Curtain
Fagui Curtain

Reputation: 1917

Working with missing values in Deedle Time Series in F# (1)

here is a small example where I want to deal with missing values on custom functions on series.

suppose that i have obtained a series

 series4;;
 val it : Series<int,int opt> =
 1 -> 1         
 2 -> 2         
 3 -> 3         
 4 -> <missing> 

for example, this way:

 let series1 = Series.ofObservations [(1,1);(2,2);(3,3)]
 let series2 = Series.ofObservations [(1,2);(2,2);(3,1);(4,4)]

 let series3 = series1.Zip(series2,JoinKind.Outer);;
 let series4 = series3 |> Series.mapValues fst

Then if i do,

 Series.mapAll (fun v -> match v with
                             | Some a -> (a>1)
                             | _-> false) series4

that fails with

 System.Exception: Operation could not be completed due to earlier error  The type 'int option' does not match the type 'int opt'. See also input.fsx(4,42)-(4,49). at 4,42

while i would like the result to be

val it : Series<int,bool opt> =
     1 -> false        
     2 -> true         
     3 -> true         
     4 -> false

even better would be to able to get a result like

val it : Series<int,int opt> =
     1 -> false         
     2 -> true         
     3 -> true         
     4 -> <missing> 

what would be the right syntax there ? ideally if there is a <missing> value, i would like a <missing> value for the same key in the new series

basically i need to do pattern matching on int opt type

Bonus question: is there are vectorized operator in Deedle for some usual operators such like ">" ? (series1 > series2) when both series have the same key types would return a new series of boolean (option ?)type

thanks

Upvotes: 4

Views: 177

Answers (1)

FoggyFinder
FoggyFinder

Reputation: 2220

You can do it this way:

let series5 =
    series4
    |> Series.mapValues(OptionalValue.map(fun x -> x > 1))

You can read about module OptionalValue in the documentation

Upvotes: 1

Related Questions