2014-09-25 66 views
1

我也注意到了有關Deedle mapRows一些奇怪的功能,我不能解釋:Deedle Frame.mapRows如何正確地使用它,以及如何構建objectseries正確

let col1 =  Series.ofObservations[1=>10.0;2=>System.Double.NaN;3=>System.Double.NaN;4=>10.0;5=>System.Double.NaN;6=>10.0; ] 

let col2 = Series.ofObservations[1=>9.0;2=>5.5;3=>System.Double.NaN;4=>9.0;5=>System.Double.NaN;6=>9.0; ] 
let f1 = Frame.ofColumns [ "c1" => col1; "c2" => col2 ] 
let f2 = f1 |> Frame.mapRows (fun k r -> r) |> Frame.ofRows 
let f3 = f1 |> Frame.mapRows (fun k r -> let x = r.Get("c1"); 
              let y = r.Get("c2"); 
              r) |> Frame.ofRows 


val f1 : Frame<int,string> = 

     c1  c2   
1 -> 10  9   
2 -> <missing> 5.5  
3 -> <missing> <missing> 
4 -> 10  9   
5 -> <missing> <missing> 
6 -> 10  9   

val f2 : Frame<int,string> = 

     c1  c2   
1 -> 10  9   
2 -> <missing> 5.5  
3 -> <missing> <missing> 
4 -> 10  9   
5 -> <missing> <missing> 
6 -> 10  9   

val f3 : Frame<int,string> = 

     c1  c2   
1 -> 10  9   
2 -> <missing> <missing> 
3 -> <missing> <missing> 
4 -> 10  9   
5 -> <missing> <missing> 
6 -> 10  9   

如何F3具有比F2不同的價值?我用f3做的所有事情都是爲了從obejectseries中獲得價值。

我想使用這個mapRows函數來做基於行的過程併產生一個對象然後mapRows可以用相同的行鍵創建一個新的框架。該過程必須是基於行的,因爲列值需要根據其自己的值和相鄰值進行更新。

計算不能直接使用列到列來完成,因爲計算會根據行值進行更改。

欣賞任何建議

更新

由於原來的問題被張貼,因爲我已經用過Deedle在C#。令我驚訝的是,基於行的計算在C#中非常容易,C#Frame.rows函數處理缺失值的方式與F#mapRows函數非常不同。以下是我用來嘗試真正邏輯的一個非常基本的例子。對於任何正在搜索類似應用程序的人可能都有用:

需要注意的事項有: 1.行功能沒有刪除行,但兩列的值都丟失 2.平均值函數是智能足以根據可用數據點計算平均值。

using System.Text; 
using System.Threading.Tasks; 
using Deedle; 

namespace TestDeedleRowProcessWithMissingValues 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var s1 = new SeriesBuilder<DateTime, double>(){ 
       {DateTime.Today.Date.AddDays(-5),10.0}, 
       {DateTime.Today.Date.AddDays(-4),9.0}, 
       {DateTime.Today.Date.AddDays(-3),8.0}, 
       {DateTime.Today.Date.AddDays(-2),double.NaN}, 
       {DateTime.Today.Date.AddDays(-1),6.0}, 
       {DateTime.Today.Date.AddDays(-0),5.0} 
      }.Series; 

      var s2 = new SeriesBuilder<DateTime, double>(){ 
       {DateTime.Today.Date.AddDays(-5),10.0}, 
       {DateTime.Today.Date.AddDays(-4),double.NaN}, 
       {DateTime.Today.Date.AddDays(-3),8.0}, 
       {DateTime.Today.Date.AddDays(-2),double.NaN}, 
       {DateTime.Today.Date.AddDays(-1),6.0}     
      }.Series; 

      var f = Frame.FromColumns(new KeyValuePair<string, Series<DateTime, double>>[] { 
       KeyValue.Create("s1",s1), 
       KeyValue.Create("s2",s2) 
      }); 

      s1.Print(); 
      f.Print(); 


      f.Rows.Select(kvp => kvp.Value).Print(); 

//   29/05/2015 12:00:00 AM -> series [ s1 => 10; s2 => 10] 
//   30/05/2015 12:00:00 AM -> series [ s1 => 9; s2 => <missing>] 
//   31/05/2015 12:00:00 AM -> series [ s1 => 8; s2 => 8] 
//   1/06/2015 12:00:00 AM -> series [ s1 => <missing>; s2 => <missing>] 
//   2/06/2015 12:00:00 AM -> series [ s1 => 6; s2 => 6] 
//   3/06/2015 12:00:00 AM -> series [ s1 => 5; s2 => <missing>] 


      f.Rows.Select(kvp => kvp.Value.As<double>().Mean()).Print(); 

//   29/05/2015 12:00:00 AM -> 10 
//   30/05/2015 12:00:00 AM -> 9 
//   31/05/2015 12:00:00 AM -> 8 
//   1/06/2015 12:00:00 AM -> <missing> 
//   2/06/2015 12:00:00 AM -> 6 
//   3/06/2015 12:00:00 AM -> 5 


      //Console.ReadLine(); 
     } 
    } 
} 

回答

2

爲什麼f3從不同的方式mapRows處理缺少值的原因如下。

當您使用r.Get("C1")訪問某個值時,您可以獲取該值或獲得ValueMissingExceptionmapRows函數處理此異常並將整行標記爲缺失。如果你寫的只是:

let f3 = f1 |> Frame.mapRows (fun k r -> 
    let x = r.Get("c1"); 
    let y = r.Get("c2"); 
    r) 

那麼結果將是:

1 -> series [ c1 => 10; c2 => 9] 
2 -> <missing>     
3 -> <missing>     
4 -> series [ c1 => 10; c2 => 9] 
5 -> <missing>     
6 -> series [ c1 => 10; c2 => 9] 

如果你想編寫一個返回,因爲它是(讀取來自原始行的數據,併產生新的行幀的功能),你可以這樣做:

f1 
|> Frame.mapRows (fun k r -> 
    [ "X" => OptionalValue.asOption(r.TryGet("c1")); 
    "Y" => OptionalValue.asOption(r.TryGet("c2")) ] 
    |> Series.ofOptionalObservations) 
|> Frame.ofRows 
相關問題