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

Posted

技术标签:

【中文标题】时间戳未显示在 Python 的 yfinance 包中【英文标题】:Timestamp not showing up in yfinance package in Python 【发布时间】:2020-07-22 23:35:25 【问题描述】:

我正在尝试使用 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。我可以对时间戳进行后处理,因为我知道间隔。只是好奇我是否在这里做错了什么。包应该是这样工作的吗?

【问题讨论】:

【参考方案1】:

对于不熟悉 yfinance 的人,这是从 yfinance history() 函数中提取数据的更详细信息。

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

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

有对 Pandas 数据结构 here 和 here 的深入描述。

DataFrame 对象中的每一个 都是一个 Pandas Series 对象,它就像一个一维数组。可以通过 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

【讨论】:

【参考方案2】:

您是否尝试过使用“60m”作为间隔参数?似乎有一个问题你可以在这里看到:https://github.com/ranaroussi/yfinance/issues/125

【讨论】:

以上是关于时间戳未显示在 Python 的 yfinance 包中的主要内容,如果未能解决你的问题,请参考以下文章

python的yfinance股票历史记录不起作用

使用 python yfinance 多线程下载雅虎股票历史

量化交易必备的开源项目:yfinance

yfinance 中的错误

yfinance 和 yahoo 财务数据非常不同

时间戳未打印在 FFMPEG 的缩略图上