python - 当我从 api (yfinance) 获取数据时,Numpy Array 缺少日期
问题描述
我正在尝试在 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
我可以重置数据帧索引df.reset_index()
并能够访问日期。为什么我无法访问数组中的日期,解决方法是什么?我不想使用数据帧,因为数组要快得多。
解决方案
我遇到了同样的问题,这些是周末和节假日,市场在这些日期关闭。转换为 numpy 数组是有问题的,因为日期是或者datetime
其余string
数据是float
. 要保留第一列,您需要将其转换为float
或int
使用儒略日函数https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timestamp.to_julian_date.html
推荐阅读
- reactjs - React material-ui中Tab、TabContext、Tablist、TabPanel的区别
- node.js - 如何在不删除服务器生成的文件的情况下部署 dist 文件夹
- python - 逐列检查数组中的元素是否存在于另一个数组中
- python - Mac 上 CPLEX Python API 安装大问题 CpoException
- amazon-web-services - 为什么我不能通过 EC2 (AWS) 访问我托管的 Flask webApp
- swift - 试图将元素附加到矩阵中的向量时卡住了
- php - Wordpress 如何将自定义类添加到作者名称以进行样式设置
- react-router-dom - 白页 react-router-dom
- python - Scrapy不解析数据
- python - Django:如何应用 uuid django.contrib.auth 迁移