【问题标题】:How do I iterate over a Deedle Series<DateTime,Series<DateTime,ObjectSeries<string>>如何迭代 Deedle Series<DateTime,Series<DateTime,ObjectSeries<string>>
【发布时间】:2016-04-07 00:02:01
【问题描述】:

我已经使用

创建了一个数据框
// Create a dataframe containing the Open, High, Low, and Close
let ohlc =
cl 
|> Frame.sliceCols ["Open"; "High"; "Low"; "Close"; "Volume"]

结果输出: 开高低收盘量 2014 年 12 月 28 日晚上 8:00:00 -> 62.13 62.67 62.13 62.27 3206
2014 年 12 月 28 日晚上 9:00:00 -> 62.27 62.42 62.14 62.39 1620
2014 年 12 月 28 日晚上 10:00:00 -> 62.4 62.41 62.16 62.21 1275
2014 年 12 月 28 日晚上 11:00:00 -> 62.21 62.32 61.96 62.19 2791
12/29/2014 12:00:00 AM -> 62.17 62.25 62.08 62.23 1233
2014 年 12 月 29 日凌晨 1:00:00 -> 62.23 62.41 62.21 62.31 1186
2014 年 12 月 29 日凌晨 2:00:00 -> 62.32 62.32 62.07 62.21 1446
2014 年 12 月 29 日凌晨 3:00:00 -> 62.22 62.35 62.17 62.28 1335

我现在想从上述每小时样本中生成更高的时间范围(每天)。

我开始:

ohlc
|> Frame.rows
|> Series.resampleEquiv (fun d -> d.Date)

返回: Series&lt;DateTime,Series&lt;DateTime,ObjectSeries&lt;string&gt;&gt;&gt;.

我想创建一个新的 DataFrame,其中包含 Date(key)、Open、High、Low、Close 和 Volume 列。打开是该系列第 1 行中的第一个打开。 High 是该系列中的 Max High。低是该系列中的最低低。 Close 是该系列中的最后一个 Close。 Volume是系列中Volume的总和

比如:

ohlc
|> Frame.rows
|> Series.resampleEquiv (fun d -> d.Date)
|> ??
|> ??

与其尝试使用 Rows 在 Frame 级别执行此操作,不如尝试使用 Columns 使用 Frame 执行此操作?

更新 这是完成的代码:

ohlc
|> Frame.rows
|> Series.resampleEquiv (fun d -> d.Date)
|> Series.mapValues (fun s ->
   let temp = Frame.ofRows s 
   series ["Open"  => Series.firstValue temp?Open
           "High"  => defaultArg (Stats.max temp?High) nan
           "Low"   => defaultArg (Stats.min temp?Low) nan
           "Close" => Series.lastValue temp?Close 
           "Volume" => defaultArg (Some( Stats.sum temp?Volume) ) nan ] )
|> Frame.ofRows

我无法使用:

"Volume" => defaultArg (Stats.sum temp?Volume) nan ] )

因为这给了我一个错误消息:这个表达式应该有类型浮点选项,但这里有浮点类型。我不得不包装函数 Some()。不知道为什么 Stats.sum 需要这个,但 Stat.max 和 Stats.min 不需要。

【问题讨论】:

    标签: f# deedle fslab


    【解决方案1】:

    调用resampleEquiv后,你会得到一系列对象系列(表示原始帧的不同列)系列(表示不同时间但相同日期的值)的系列(表示具有相同日期的块) .

    您可以遍历顶级系列并将每个系列的对象系列(每个块)转回一个框架。然后你可以在框架上进行聚合并返回一个新行:

    source
    |> Series.resampleEquiv (fun d -> d.Date.Year)
    |> Series.mapValues (fun s -> 
        let temp = Frame.ofRows s
        series [ "Open" => Series.firstValue temp?Open
                 "High" => defaultArg (Stats.max temp?High) nan ])
    |> Frame.ofRows
    

    我这样做只是为了开放和高,但你可以看到这个想法:-)。在每个块上调用Frame.ofRows 也应该相当快,因为​​ Deedle 知道块中的所有项目都具有相同的索引。 (或者,您可以遍历各个行,但这会使其更长)。

    【讨论】:

      猜你喜欢
      • 2016-07-19
      • 2021-12-22
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 2021-09-15
      • 2021-10-18
      • 2014-12-03
      • 2010-12-23
      相关资源
      最近更新 更多