如何使用 Excel 的日期作为键索引 Deedle Frame?
How to index a Deedle Frame with Excel's dates as keys?
假设我得到一列 "Date",其值如下所示:
86 年 3 月 10 日、86 年 6 月 10 日、86 年 7 月 10 日等...
并不像Frame.indexRowsDate("Date")
那么简单。
我目前的解决方案是在 Excel 上创建 3 个额外的列:
- 年
- 月份
- 天
具有值:
- =年份(A2)
- =月份(A2)
- =日(A2)
(对于第 2 行,其中 A 是包含日期的列)
然后使用这个函数:
let toDateTime (os:ObjectSeries<_>) =
let year = (os.Get "Year") :?> int)
let month = (os.Get "Month" :?> int)
let day = (os.Get "Day" :?> int)
DateTime(year,month,day)
Frame.indexRowsUsing toDateTime frame
解决方案
根据提供的答案,新的 toDateTime
看起来像这样:
let toDateTime (os:ObjectSeries<_>) =
DateTime.Parse((os.Get "Date") :?> string)
您不能使用 DateTime 的构造函数并为其提供字符串。
let d = new DateTime("03/10/86")
但是你可以使用 static member TryParse
它将 return 一个元组给你
let (success, date) = DateTime.TryParse("03/10/86")
if success then
//use date here
else
//do some error handling
假设我得到一列 "Date",其值如下所示: 86 年 3 月 10 日、86 年 6 月 10 日、86 年 7 月 10 日等...
并不像Frame.indexRowsDate("Date")
那么简单。
我目前的解决方案是在 Excel 上创建 3 个额外的列:
- 年
- 月份
- 天
具有值:
- =年份(A2)
- =月份(A2)
- =日(A2)
(对于第 2 行,其中 A 是包含日期的列)
然后使用这个函数:
let toDateTime (os:ObjectSeries<_>) =
let year = (os.Get "Year") :?> int)
let month = (os.Get "Month" :?> int)
let day = (os.Get "Day" :?> int)
DateTime(year,month,day)
Frame.indexRowsUsing toDateTime frame
解决方案
根据提供的答案,新的 toDateTime
看起来像这样:
let toDateTime (os:ObjectSeries<_>) =
DateTime.Parse((os.Get "Date") :?> string)
您不能使用 DateTime 的构造函数并为其提供字符串。
let d = new DateTime("03/10/86")
但是你可以使用 static member TryParse
它将 return 一个元组给你
let (success, date) = DateTime.TryParse("03/10/86")
if success then
//use date here
else
//do some error handling