繁体   English   中英

如何访问熊猫的DataReader中的日期行?

[英]How to access date row in Pandas' DataReader?

我正在使用Python的Pandas从Yahoo Finance获取一些财务数据,这是pandas_datareader的内置功能。 我希望访问它打印出的日期,但是它似乎不在列中,也不在我需要的json中,但是当我打印出对象时就在那里:

from pandas_datareader.data import DataReader
import datetime

start = datetime.datetime(2015, 6, 1)
goog = DataReader('GOOGL', 'yahoo', start)
goog.to_json(None, orient="records") # this doesn't include dates!
print(goog) # this prints out the dates along with the other data!
print(goog.columns) # prints every column except the date column

如何在JSON字符串中包含日期以及其他数据?

list(goog.index)提供日期作为时间戳列表。

为了获取json中的日期,我快速浏览了docs 尝试这个:

print goog.to_json(orient="index", date_format='iso') 

熊猫数据帧具有用于分组和快速查找的索引。 索引不被视为列之一。 要将日期从索引移动到列,可以使用reset_index() ,这将使Date成为列,并使索引只是一个从0开始计数的数字序列。

因此,要导出为包含日期的JSON格式:

goog.reset_index().to_json(None, orient='records', date_format='iso')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM