Python 中的 yfinance 包中未显示时间戳

Timestamp not showing up in yfinance package in Python

我正在尝试使用 yfinance 软件包通过 Yahoo Finance API 以 1 小时为间隔提取股票价格历史记录。我运行下面的代码。

import yfinance as yf
msft = yf.Ticker("MSFT")
df = msft.history(period = "5d", interval = "1h")
df.reset_index(inplace = True)
print(df["Date"][0])
print(df["Date"][1])
print(df["Date"][2])

我得到了输出

2020-04-03 00:00:00
2020-04-03 00:00:00
2020-04-03 00:00:00

为什么时间戳都是00:00:00?股票价格实际上以 1 小时为间隔,看起来是正确的。 7 行后日期也会正确更改。只是时间戳全为0。我可以只对时间戳进行后处理,因为我知道时间间隔。只是好奇我是否在这里做错了什么。这就是包的工作方式吗?

您试过使用“60m”作为间隔参数吗?您可以在此处看到一个问题:https://github.com/ranaroussi/yfinance/issues/125

对于那些刚接触 yfinance 的人来说,这是如何从 yfinance history() 函数中提取数据的更详细信息。

yfinance 使用名为 Pandas 的模块。从 yfinance API 返回的数据结构是 Pandas 个对象。

history()函数返回的对象是一个PandasDataFrame对象。它们就像二维数组,还有一些额外的东西。 对于 DataFrame 对象,有一个包含列名数组的 columns 字段和一个包含适用于列的 索引对象数组 index 字段.索引是固定类型的,可以是对象本身。在 yfinance history() 函数返回的 DataFrame 对象中,索引是 Pandas Timestamp 对象。 (Pandas 允许使用任何类型的索引,例如纯整数或字符串或其他对象也将被允许)

in-depth 描述了 Pandas 个数据结构 here and here

DataFrame 对象中的每个 都是一个Pandas 系列对象,类似于一维数组。可以通过 DataFrame 对象中的列名访问这些列。可以使用索引对象访问每一列中的列值。每列使用相同的索引。 Python 数组符号 [ ] 可用于访问 Pandas 对象中的字段。

这是访问数据的方法:

def zeroX(n):
    result = ""
    if (n < 10):
        result += "0"
    result += str (n)
    return result

def dump_Pandas_Timestamp (ts):
    result = ""
    result += str(ts.year) + "-" + zeroX(ts.month) + "-" + zeroX(ts.day)
    #result += " " + zeroX(ts.hour) + ":" + zeroX(ts.minute) + ":" + zeroX(ts.second)
    return result

def dump_Pandas_DataFrame (DF):
    result = ""
    for indexItem in DF.index:
        ts = dump_Pandas_Timestamp (indexItem)
        fields = ""
        first = 1
        for colname in DF.columns:
            fields += ("" if first else ", ") + colname + " = " + str(DF[colname][indexItem])
            first = 0
        result += ts + " " + fields + "\n"
    return result
    
msft = yf.Ticker("MSFT")
    
# get historical market data
hist = msft.history(period="1mo", interval="1d")
    
print ("hist = " + dump_Pandas_DataFrame(hist))

输出:

hist = 2020-07-08 Open = 210.07, High = 213.26, Low = 208.69, Close = 212.83, Volume = 33600000, Dividends = 0, Stock Splits = 0
2020-07-09 Open = 216.33, High = 216.38, Low = 211.47, Close = 214.32, Volume = 33121700, Dividends = 0, Stock Splits = 0
2020-07-10 Open = 213.62, High = 214.08, Low = 211.08, Close = 213.67, Volume = 26177600, Dividends = 0, Stock Splits = 0
2020-07-13 Open = 214.48, High = 215.8, Low = 206.5, Close = 207.07, Volume = 38135600, Dividends = 0, Stock Splits = 0
2020-07-14 Open = 206.13, High = 208.85, Low = 202.03, Close = 208.35, Volume = 37591800, Dividends = 0, Stock Splits = 0
2020-07-15 Open = 209.56, High = 211.33, Low = 205.03, Close = 208.04, Volume = 32179400, Dividends = 0, Stock Splits = 0
2020-07-16 Open = 205.4, High = 205.7, Low = 202.31, Close = 203.92, Volume = 29940700, Dividends = 0, Stock Splits = 0
2020-07-17 Open = 204.47, High = 205.04, Low = 201.39, Close = 202.88, Volume = 31635300, Dividends = 0, Stock Splits = 0
2020-07-20 Open = 205.0, High = 212.3, Low = 203.01, Close = 211.6, Volume = 36884800, Dividends = 0, Stock Splits = 0
2020-07-21 Open = 213.66, High = 213.94, Low = 208.03, Close = 208.75, Volume = 38105800, Dividends = 0, Stock Splits = 0
2020-07-22 Open = 209.2, High = 212.3, Low = 208.39, Close = 211.75, Volume = 49605700, Dividends = 0, Stock Splits = 0
2020-07-23 Open = 207.19, High = 210.92, Low = 202.15, Close = 202.54, Volume = 67457000, Dividends = 0, Stock Splits = 0
2020-07-24 Open = 200.42, High = 202.86, Low = 197.51, Close = 201.3, Volume = 39827000, Dividends = 0, Stock Splits = 0
2020-07-27 Open = 201.47, High = 203.97, Low = 200.86, Close = 203.85, Volume = 30160900, Dividends = 0, Stock Splits = 0
2020-07-28 Open = 203.61, High = 204.7, Low = 201.74, Close = 202.02, Volume = 23251400, Dividends = 0, Stock Splits = 0
2020-07-29 Open = 202.5, High = 204.65, Low = 202.01, Close = 204.06, Volume = 19632600, Dividends = 0, Stock Splits = 0
2020-07-30 Open = 201.0, High = 204.46, Low = 199.57, Close = 203.9, Volume = 25079600, Dividends = 0, Stock Splits = 0
2020-07-31 Open = 204.4, High = 205.1, Low = 199.01, Close = 205.01, Volume = 51248000, Dividends = 0, Stock Splits = 0
2020-08-03 Open = 211.52, High = 217.64, Low = 210.44, Close = 216.54, Volume = 78983000, Dividends = 0, Stock Splits = 0
2020-08-04 Open = 214.17, High = 214.77, Low = 210.31, Close = 213.29, Volume = 49280100, Dividends = 0, Stock Splits = 0
2020-08-05 Open = 214.9, High = 215.0, Low = 211.57, Close = 212.94, Volume = 28858600, Dividends = 0, Stock Splits = 0
2020-08-06 Open = 212.34, High = 216.37, Low = 211.55, Close = 216.35, Volume = 32656800, Dividends = 0, Stock Splits = 0
2020-08-07 Open = 214.85, High = 215.7, Low = 210.93, Close = 212.48, Volume = 27789600, Dividends = 0, Stock Splits = 0