我试图在一个Numpy数组中收集股票数据,其中股票价格的日期在第一列。当我将数据直接转换成数组时,我将得到[ 30.99 32.08 32.12 ... 318.66 315.77 323.5 ]
下面是我的代码
import numpy as np
import yfinance as yf
def price(ticker):
company = yf.Ticker(ticker)
price = company.history(period="10y")
array = np.array(price)
return array
aapl = price("AAPL")
Aaple = aapl[:, 0]
如果我使用数据帧,我会得到这个
def price(ticker):
company = yf.Ticker(ticker)
price = company.history(period="10y")
df = pd.DataFrame(price)
df.drop(df.columns[i], axis=1)
return df
aapl = price("AAPL")
print(aapl)
Open High Low Close Volume Dividends Stock Splits
Date
2010-05-27 30.99 31.40 30.81 31.33 166570600 0.00 0.0
2010-05-28 32.08 32.08 31.33 31.77 203903700 0.00 0.0
... ... ... ... ... ... ... ...
2020-05-22 315.77 319.23 315.35 318.89 20450800 0.00 0.0
2020-05-26 323.50 324.20 316.50 316.73 30880340 0.00 0.0
我可以重置dataframes索引df.reset_index()
,并能够访问日期。为什么我无法访问数组中的日期,修复方法是什么?我不想使用数据帧,因为数组要快得多
我也有同样的问题,这些是周末和假期,市场在这些日期关闭。转换为numpy数组是有问题的,因为日期是
datetime
或string
,而其余数据是float
。要保留第一列,需要将其转换为float
或int
,例如,使用朱利安日函数https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timestamp.to_julian_date.html相关问题 更多 >
编程相关推荐