正在尝试将数据帧转换为预期格式。与ds有关的问题

2024-04-25 12:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

#Covnvert the dataframe into the prophet expected format.
apple.rename(columns={'close': 'y', 'date': 'ds'},inplace=True)
apple['ds'] = pd.to_datetime(apple['ds'])
apple['y']=apple['y'].astype(float)

错误输出如下:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2888             try:
-> 2889                 return self._engine.get_loc(casted_key)
   2890             except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
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: 'ds'
The above exception was the direct cause of the following exception:
KeyError                                  Traceback (most recent call last)
<ipython-input-5-67265c944d5c> in <module>
      1 #Covnvert the dataframe into the prophet expected format.
      2 apple.rename(columns={'close': 'y', 'date': 'ds'},inplace=True)
----> 3 apple['ds'] = pd.to_datetime(apple['ds'])
      4 apple['y']=apple['y'].astype(float)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2897             if self.columns.nlevels > 1:
   2898                 return self._getitem_multilevel(key)
-> 2899             indexer = self.columns.get_loc(key)
   2900             if is_integer(indexer):
   2901                 indexer = [indexer]
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2889                 return self._engine.get_loc(casted_key)
   2890             except KeyError as err:
-> 2891                 raise KeyError(key) from err
   2892 
   2893         if tolerance is not None:
KeyError: 'ds"*

Tags: columnsthekeyinselfapplepandasget