Advertisement
XperiAndri

DataFrame with fixed columns

Dec 22nd, 2021
3,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.53 KB | None | 0 0
  1.  
  2. open System
  3. open System.IO
  4. open Microsoft.Data.Analysis
  5.  
  6. let columns : DataFrameColumn array =
  7.     [|
  8.         UInt16DataFrameColumn("VNR", 6L)
  9.         ByteDataFrameColumn("Volume", 10L)
  10.         SingleDataFrameColumn("Value", 12L)
  11.         ByteDataFrameColumn("Farmacy number", 4L)
  12.         PrimitiveDataFrameColumn<DateTime>("Date of sale", 8L)
  13.         CharDataFrameColumn("Sale period", 1L)
  14.         ByteDataFrameColumn("Hospital number", 7L)
  15.         StringDataFrameColumn("Delivery address", 4L)
  16.         StringDataFrameColumn("Wholesaler", 9L)
  17.     |]
  18.  
  19. use stream =
  20.     new FileStream(
  21.         @"file.txt",
  22.         FileMode.Open, FileAccess.Read, FileShare.Read)
  23.  
  24. let reader = new TextFieldParser (stream)
  25. reader.TextFieldType <- FieldType.FixedWidth
  26. let widths = columns |> Array.map (fun c -> c.Length |> int)
  27. reader.SetFieldWidths widths
  28.  
  29. let df = DataFrame columns
  30.  
  31. while not <| reader.EndOfData do
  32.     try
  33.         let currentRow = reader.ReadFields ()
  34.         df.Append(currentRow |> Seq.map box, true) |> ignore
  35.         //printfn "%A" currentRow
  36.     with ex -> printfn "%s" ex.Message
  37.  
  38. Console.WriteLine df
  39.  
  40. (*
  41. Line let df = DataFrame columns
  42. System.ArgumentException: Column lengths are mismatched (Parameter 'column')
  43.    at Microsoft.Data.Analysis.DataFrameColumnCollection.InsertItem(Int32 columnIndex, DataFrameColumn column)
  44.    at Microsoft.Data.Analysis.DataFrameColumnCollection..ctor(IEnumerable`1 columns, Action columnsChanged)
  45.    at Microsoft.Data.Analysis.DataFrame..ctor(DataFrameColumn[] columns)
  46. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement