I am trying to use the Series.windowWhile function to window over rows of a Deedle frame. I want to calculate some statistics based on the window and then add the statistic back to the row.
The below code does not work once I get to the point of the last.Merge(length)
line but it hopefully illustrates my goal:
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
I want newPeople
to be the people
frame with a new column called length
.
You problem is that v
is an ObjectSeries<string>
if all you need is to add the calculated length from your mapValues
you can do it this way:
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')