在 F# 中处理 Deedle 时间序列中的缺失值(一)

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

这是一个小例子,我想在其中处理系列自定义函数的缺失值。

假设我得到了一个系列

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

例如,这样:

 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

那么如果我这样做了,

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

失败

 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

虽然我希望结果是

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

更好的是能够得到像

这样的结果
val it : Series<int,int opt> =
     1 -> false         
     2 -> true         
     3 -> true         
     4 -> <missing> 

正确的语法是什么?理想情况下,如果有一个 <missing> 值,我想要一个 <missing> 新系列中相同键的值

基本上我需要对 int opt 类型进行模式匹配

额外的问题:Deedle 中是否有一些常用运算符(如“>”)的矢量化运算符? (series1 > series2) 当两个系列具有相同的键类型时 return 一个新系列的布尔值 (option ?)type

谢谢

你可以这样做:

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

您可以在 documentation

中阅读有关模块 OptionalValue 的信息