我在我的文件中写了相同名称的日期列,但它仍然是错误

I wrote same name of date column in my files but it's still errors

提问人:wxrynz 提问时间:11/6/2023 更新时间:11/7/2023 访问量:38

问:

我查了一下这个: KeyError: 'Date' 我该如何修复它? 我想我写了日期列的名称,代表日期与我的文件相同,但它仍然是错误的

这是我的代码:

!pip install yfinance
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import yfinance as yf

# Load your historical data into a DataFrame (make sure to have a 'Date' and 'Close' column)
# You may need to preprocess your data to have the required columns
data = yf.download("^SET.BK", start="2012-01-01", end="2023-07-01")
date_column_name = 'Date'
close_column_name = 'Close'


data[date_column_name] = pd.to_datetime(data[date_column_name])

这是错误

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3801             try:
-> 3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:

4 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:
-> 3804                 raise KeyError(key) from err
   3805             except TypeError:
   3806                 # If we have a listlike key, _check_indexing_error will raise
Python pandas 随机森林

评论


答:

0赞 jeste_artyste 11/7/2023 #1

Yfinance lib,将列“Date”作为索引。如果直接从 yfinance 查看输出,您会看到“日期”列低于其他列标题。此外,您可以检查,使用方法 data.info() 它会显示“日期”作为索引。将“日期”作为普通列的简单事情是

data.reset_index(inpalce=True)

它应该可以解决您的问题:)

评论

0赞 wxrynz 11/7/2023
多谢。但是你知道为什么我训练模型和每天得到的预测值是一样的吗?
0赞 jeste_artyste 11/7/2023
您没有为任何类型的模型训练提供代码,因此您的问题与关键错误有关,因此基本上是 DataFrame 中列名的值。如果您对模型本身有进一步的问题,我会创建另一个仅与模型训练相关的查询