【发布时间】:2021-11-22 23:42:02
【问题描述】:
我有以下设置:
我想将来自 yfinance Dataframe 的数据存储在代表 X 和 Y 值的两个列表中。这是通过以下几行实现的:
import yfinance as yf
import matplotlib.pyplot as plt
stock = 'TSLA'
start = '2020-11-01'
df = yf.download(stock , start=start)
nfx = []
nfy = []
for index, row in df.iterrows():
x_data = nfx.append(index)
y_data = nfy.append(row['Close'])
现在我想将这些 X 和 Y 值输入到 Polyfit 函数中。 为此,我需要以某种方式将它们设为整数。
我怎样才能实现这个想法? 当我尝试以下方法时:
for index, row in df.iterrows():
x_data = nfx.append(np.asarray(index).astype(float))
y_data = nfy.append(row['Close'])
,我收到错误消息:
TypeError: float() argument must be a string or a number, not 'Timestamp'
我在这里错过了什么?
非常感谢, 本杰明
【问题讨论】:
-
不要使用循环来填充 x_data 和 y_data。你可以做
x_data = df.index和y_data = df['Close']。你的数据框的索引是什么类型的?根据该类型,您有不同的可能性将日期更改为数字 -
好的,谢谢,这似乎已经是一种改进。但是我需要将时间戳转换为整数;)
标签: python dataframe timestamp integer typeerror