Skip to content

Commit

Permalink
Merge pull request #406 from zyzhu/issue-364
Browse files Browse the repository at this point in the history
Avoid iterating seq multiple times in Frame.ofRecords
  • Loading branch information
zyzhu authored Mar 21, 2019
2 parents 4afb105 + c05321b commit 3d055db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Deedle/FrameUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,11 @@ module internal Reflection =
[| for _, (input, body) in convertors ->
let cast = Expression.Convert(body, typeof<IVector>)
Expression.Lambda<Func<seq<'T>, IVector>>(cast, input).Compile() |]
let data = data |> Array.ofSeq
let frameData =
[| for convFunc in convertors -> convFunc.Invoke(data) |]
|> vectorBuilder.Create
Frame<int, string>(Index.ofKeys [0 .. (Seq.length data) - 1], colIndex, frameData, IndexBuilder.Instance, VectorBuilder.Instance)
Frame<int, string>(Index.ofKeys [0 .. data.Length - 1], colIndex, frameData, IndexBuilder.Instance, VectorBuilder.Instance)

/// Helper that makes it possible to call convertRecordSequence on untyped enumerable
type ConvertRecordHelper =
Expand Down
12 changes: 12 additions & 0 deletions tests/Deedle.Tests/Frame.fs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ let ``Can read simple sequence of records using a specified column as index`` ()
set df.ColumnKeys |> shouldEqual (set ["Volume"; "Price"])
df.RowKeys |> Seq.head |> shouldEqual rows.[0].Date

[<Test>]
let ``Can read simple sequence of records deterministically `` () =
let mutable n = 0
let values =
seq {
yield n, n
n <- n + 1
yield n, n
n <- n + 1 }
let expected = [|0, 0; 1, 1|] |> Frame.ofRecords
values |> Frame.ofRecords |> shouldEqual expected

[<Test>]
let ``Can expand properties of a simple record sequence`` () =
let df = frame [ "MSFT" => Series.ofValues (typedRows ()) ]
Expand Down

0 comments on commit 3d055db

Please sign in to comment.