2017-05-17 45 views
3

這裏是一個小例子,我想處理系列上自定義函數的缺失值。在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

回答

1

你可以這樣來做(選項):

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

你可以閱讀關於模塊OptionalValuedocumentation

相關問題