首页 > 解决方案 > 将时间序列从 CSV 加载到 DataFrame

问题描述

是否可以从第一列是一系列日期的 CSV 创建一个 Daru DataFrame?

以下面的 CSV 为例:

time,min,max
2018-01-01,101,103
2018-01-02,102,105
2018-01-03,103,200
2018-01-04,104,109
2018-01-05,105,110

如果加载Daru::DataFrame.from_csv它,将创建一个 5x3 DataFrame 和一个从 0 开始的数字索引,而不是一个带有 DateTimeIndex 的 5x2 DataFrame。

有没有办法指示 Daru 使用第一个向量作为DateTimeIndex索引?

标签: dataframescirubydaru

解决方案


df = Daru::DataFrame.from_csv("df.csv")
df.set_index "time"
df.index = Daru::DateTimeIndex.new(df.index)
df
<Daru::DataFrame(5x2)>
                  min        max
2018-01-01        101        103
2018-01-02        102        105
2018-01-03        103        200
2018-01-04        104        109
2018-01-05        105        110

推荐阅读