如果我通过使用 .Rows.[rowIndex]
操作获得 Frame
的 row
,Deedle
将返回给我一个对象系列
。有时我知道这只包含float
。如何在拍摄时将所有 obj
转换为 float
系列?
最佳答案
在 Deedle 系列中是通用的,因此理想情况下应该可以立即获得 float 系列。但由于您获得一系列对象的原因尚不清楚,您仍然可以通过映射适当的类型转换函数将值转换为 float :
#load @"..\packages\Deedle.1.2.4\Deedle.fsx"
open Deedle
open System
// Let's prepare a sample series
let keys = ["1";"2";"3"]
let values = [1.1 :> Object;1.2 :> Object;1.3 :> Object]
let series = Series(keys, values)
// Now apply the map taking the Series<string,System.Object> series to Series<string,float>
series |> Series.map (fun _ v -> v :?> float)
// as @Foggy Finder pointed out, there is a convenience function to only map values
series |> Series.mapValues (fun v -> v :?> float)
// Alternatively, use the tryMap function that takes the Series<int,Object> series
// to Series<int,TryValue<float>>
series |> Series.tryMap (fun _ v -> v :?> float)
Series.map
的类型功能是 (('a -> 'b -> 'c) -> Series<'a,'b> -> Series<'a,'c>) when 'a : equality
。这意味着映射函数的第一个参数是我们使用下划线忽略的键,因为不需要进行类型转换。正如Foggy Finder指出的那样,有一个方便的功能Series.mapValues
隐藏 key 。
关于F# deedle 将 Series<string, obj> 转换为 Series<string, float>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826274/