对于那些不熟悉 yfinance 的人来说,这是如何history()
更详细地从 yfinance 函数中提取数据的方法。
yfinance 使用一个名为 Pandas 的模块。从 yfinance API 返回的数据结构是 Pandas 对象。
该函数返回的对象history()
是 Pandas DataFrame 对象。它们就像二维数组,带有附加值。对于 DataFrame 对象,有一个columns
包含列名数组的字段,以及一个index
包含适用于列的索引对象数组的字段。索引是固定类型的,并且可以是对象本身。在 yfinance 函数返回的 DataFrame 对象中history()
,索引是 Pandas Timestamp 对象。(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