我正在尝试使用yfnance包通过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
对于那些刚接触yfnance的人来说,这是如何从yfnancehistory()
函数中更详细地提取数据的。
yfinance使用一个名为Pandas的模块。从yfnance API返回的数据结构是Pandas对象。
history()
函数返回的对象是Pandas DataFrame对象。它们就像2维数组,有额外的元素。对于DataFrame对象,有一个包含列名数组的columns
字段和一个包含适用于列的索引对象数组的index
字段。索引是固定类型的,并且可以是对象本身。在yfnancehistory()
函数返回的DataFrame对象中,索引是Pandas时间戳对象。(Pandas允许使用任何类型的索引,例如,也允许使用纯整数或字符串或其他对象(
这里和这里都有对Pandas数据结构的深入描述。
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