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

痴心易碎 提交于 2020-01-24 10:21:06

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/44021249/working-with-missing-values-in-deedle-time-series-in-f-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!