如果我得到一个row
的Frame
使用.Rows.[rowIndex]
操作,Deedle
将返回我的Object Series
。有时我知道这只包含float
。我该如何将所有这些obj
转换成float
连续拍摄?
在Deedle系列中是通用的,因此理想情况下应该可以立即获得float系列。但是由于不清楚获得一系列Object的原因,您仍然可以通过映射适当的类型转换函数将值转换为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
可以隐藏键。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句