Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# deedle cast column datatype

Tags:

f#

deedle

I have load a csv file to a Frame, deedle automatically infers that one column as decimal, whichi in fact should be int.

I have use the following line to do the casting into the correct type,

df?ColumnName <- df.GetColumn<int>("ColumnName")

I am wondering if this is the right way.

like image 263
tesla1060 Avatar asked Nov 30 '25 23:11

tesla1060


1 Answers

You can control the type of columns when reading .csv.

ReadCsv(...) has such parameter as schema:

schema - A string that specifies CSV schema. See the documentation for information about the schema format.

More information can be found here (in the section Controlling the column types)

Example:

.csv:

Name,Age,Comp1,Comp2
"Joe", 51, 12.1, 20.3
"Tomas", 28, 1.1, 29.3
"Eve", 2, 2.1, 40.3
"Suzanne", 15, 12.4, 26.3

F#:

let pathToCSV = "0.csv"
let schema = "Name,Age(int),Comp1,Comp2"

let loadFrame = Frame.ReadCsv(pathToCSV, schema=schema)
loadFrame.Format() |> printfn "%s"

loadFrame.ColumnTypes |> Seq.iter(printfn "%A")

Print:

     Name    Age Comp1 Comp2
0 -> Joe     51  12,1  20,3
1 -> Tomas   28  1,1   29,3
2 -> Eve     2   2,1   40,3
3 -> Suzanne 15  12,4  26,3

System.String
System.Int32
System.Decimal
System.Decimal

Although, for me, Frame have the correct column types and without specifying the schema.

like image 95
FoggyFinder Avatar answered Dec 04 '25 11:12

FoggyFinder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!