我有一个通用的 pandas 时间序列,希望将其存储在 MongoDB 中。时间序列对象 ts 如下所示:
>ts
2013-01-01 00:00:00 456.852985
2013-01-01 01:00:00 656.015532
2013-01-01 02:00:00 893.159043
...
2013-12-31 21:00:00 1116.526471
2013-12-31 22:00:00 1124.903600
2013-12-31 23:00:00 1065.315890
Freq: H, Length: 8760, dtype: float64
我希望将其转换为一个JSON文档数组,其中一个文档就是一行数据,以便存储在MongoDB中。类似于这样的形式:
[{"index": 2013-01-01 00:00:00, "col1": 456.852985},
{"index": 2013-01-01 01:00:00, "col1": 656.015532},
{"index": 2013-01-01 02:00:00, "col1": 893.159043},
...
]
我一直在研究TimeSeries.to_json()的“orient”选项,但是我无法找到获取此格式的方法。在pandas中是否有一种简单的方法执行此操作,或者我应该寻找使用外部JSON库创建此结构的方法?