在F#中使用Deedle时间系列中的缺失值(1)

问题描述:

这里是一个小例子,我想处理系列上自定义函数的缺失值。在F#中使用Deedle时间系列中的缺失值(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:由于较早的 错误,操作无法完成类型'int option'与'int opt'类型不匹配。请参阅 也input.fsx(4,42) - (4,49)。在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矢量操作对于一些常用的操作符如“>”? (系列1>系列2)当两个系列具有相同的密钥类型会返回一个新的系列布尔类型

感谢

+1

'Series.mapAll'似乎接受函数返回一个选项,在你的功能。 – yukitos

你可以这样来做(选项):

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

你可以阅读关于模块OptionalValuedocumentation