1

我还注意到 Deedle mapRows 函数有些奇怪,我无法解释:

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 所做的只是从对象系列中获得价值。

我正在尝试使用此 mapRows 函数来执行基于行的过程并生成对象序列,然后 mapRows 可以使用相同的行键创建一个新框架。该过程必须基于行,因为列值需要根据其自身值和相邻值进行更新。

由于计算会根据行值而变化,因此无法直接使用列到列来完成计算。

感谢任何建议

更新

自从发布了原始问题以来,我就在 C# 中使用了 Deedle。令我惊讶的是,在 C# 中基于行的计算非常简单,并且 C# Frame.rows 函数处理缺失值的方式与 F# mapRows 函数非常不同。以下是我用来尝试验证逻辑的一个非常基本的示例。它可能对任何正在搜索类似应用程序的人有用:

需要注意的是: 1. rows 函数没有删除行,而两列的值都丢失了 2. mean 函数足够聪明,可以根据可用数据点计算平均值。

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();
        }
    }
}
4

1 回答 1

2

不同的原因f3在于mapRows处理缺失值的方式。

当你使用 访问一个值r.Get("C1")时,你要么得到这个值,要么得到一个ValueMissingException. 该mapRows函数处理此异常并将整行标记为缺失。如果你只写:

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
于 2014-09-26T07:49:44.277 回答