我正在学习Python,现在我正在尝试检查数据库中股票价值的变化百分比。但是,我要检查的变量之一是来自Series类型的数据库。每当我尝试转换为浮点数以将其用于乘法和除法时,都会收到错误消息“ TypeError:无法将序列转换为”。我看到过声明使用.astype(float)的解决方案,但对我而言不起作用。任何帮助将不胜感激。
<class 'pandas.core.series.Series'>
2997 1131.130005
Name: Adj Close, dtype: float64
<class 'pandas.core.series.Series'>
2947 1129.439941
Name: Adj Close, dtype: float64
<class 'pandas.core.series.Series'>
2778 1198.680054
Name: Adj Close, dtype: float64
<class 'pandas.core.series.Series'>
Series([], Name: Adj Close, dtype: float64)
Traceback (most recent call last):
File "C:\Users\andre\AppData\Local\Programs\Python\Python37\SciKit-learn Tutorial\Tutorial 6 - Playing with the Data (pct_change).py", line 103, in <module>
Key_Stats()
File "C:\Users\andre\AppData\Local\Programs\Python\Python37\SciKit-learn Tutorial\Tutorial 6 - Playing with the Data (pct_change).py", line 83, in Key_Stats
sp500_pct_change = ((float(sp500_value) - starting_sp500_value) / starting_sp500_value) * 100
File "C:\Users\andre\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\series.py", line 93, in wrapper
"{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>
错误:
{{1}}
答案 0 :(得分:0)
我想我们正在同一个项目上,也在同一个路径上工作。这里是。我希望你能理解将这段代码插入哪里
try:
sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d')
row = sp500_df[sp500_df["Date"] == sp500_date]
sp500_value = row["Adj Close"]
sp500_value1 = sp500_value.values[0]
print(sp500_value1)
except:
sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d')
row = sp500_df[sp500_df["Date"] == sp500_date]
sp500_value = row["Adj Close"]
sp500_value1 = sp500_value.values[0]
print(sp500_value1)