Deedle Frame.Rows |> Series.windowWhile |> 具有从 windows 计算的新列的新框架?

Deedle Frame.Rows |> Series.windowWhile |> new frame with a new column computed from the windows?

我正在尝试使用 Series.windowWhile 函数对 Deedle 框架的行进行 window。我想根据 window 计算一些统计数据,然后将统计数据添加回行。

下面的代码在我到达 last.Merge(length) 行时不起作用,但它有望说明我的目标:

type Person = 
  { Name:string; Age:int; Countries:string list; }

let peopleRecds = 
  [ { Name = "Joe"; Age = 51; Countries = [ "UK"; "US"; "UK"] }
    { Name = "Tomas"; Age = 28; Countries = [ "CZ"; "UK"; "US"; "CZ" ] }
    { Name = "Eve"; Age = 2; Countries = [ "FR" ] }
    { Name = "Suzanne"; Age = 15; Countries = [ "US" ] } ]
// Turn the list of records into data frame 
let peopleList = Frame.ofRecords peopleRecds
// Use the 'Name' column as a key (of type string)
let people = peopleList |> Frame.indexRowsInt "Age" |> Frame.sortRowsByKey
let newPeople =
    people.Rows
    |> Series.windowWhile (fun k1 k2 -> abs (k1 - k2) < 14)
    |> Series.mapValues (fun v ->
        let length = series ["length" => (v |> Series.values |> Seq.length)]
        let last = v |> Series.takeLast 1
        let newLast = last.Merge(length)
        newLast)
    |> Frame.ofRows

我希望 newPeople 成为具有名为 length 的新列的 people 框架。

你的问题是 v 是一个 ObjectSeries<string> 如果你只需要从你的 mapValues 中添加计算的长度,你可以这样做:

let newPeople' =
    people.Rows
    |> Series.windowWhile (fun k1 k2 -> abs (k1 - k2) < 14)
    |> Series.mapValues (fun v ->
        series ["length" => (v |> Series.values |> Seq.length)]    
    )
    |> Frame.ofRows

people.Merge(newPeople')