F# deedle 将 Series<string, obj> 转换为 Series<string, float>?

标签 f# deedle

如果我通过使用 .Rows.[rowIndex] 操作获得 FramerowDeedle将返回给我一个对象系列。有时我知道这只包含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/

相关文章:

f# - 为什么我可以把 |> 放在下一行但是 <|给出语法错误?

postgresql - Npgsql.PostgresException (0x80004005) : XX000: cache lookup failed for type 207852 when schema dropped

f# - 在 F# 中并行化两个矩阵的元素乘法

c# - 如何转发填充 C# 数据框中的缺失值

f# - Deedle 标准化帧

azure - 时间序列的注意事项

visual-studio - FSLex 示例解决方案?

c# - 如何合并具有重叠行的 deedle 中的数据帧?

c# - 如何使用 Deedle 进行基于行的处理(帧输入和帧输出)

f# - 如何在 Foq 中设置返回 async<T> 的模拟?