Skip to main content
 首页 » 编程设计

python之如何访问 Pandas 的 DataReader 中的日期行

2025年05月04日36sharpest

我正在使用 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')