1

我使用创建了一个数据框

// Create a dataframe containing the Open, High, Low, and Close
let ohlc =
cl 
|> Frame.sliceCols ["Open"; "High"; "Low"; "Close"; "Volume"]

结果输出: Open High Low Close Volume 12/28/2014 8:00:00 PM -> 62.13 62.67 62.13 62.27 3206
12/28/2014 9:00:00 PM -> 62.27 62.42 62.14 62.39 1620
12/28/ 2014 10:00:00 PM -> 62.4 62.41 62.16 62.21 1275
12/28/2014 11:00:00 PM -> 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:022:00 -> 6。 62.35 62.17 62.28 1335

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

我开始:

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

返回: Series<DateTime,Series<DateTime,ObjectSeries<string>>>.

我想创建一个新的 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 不需要。

4

1 回答 1

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-04-07T11:06:28.763 回答