繁体   English   中英

在处理上述异常的过程中,发生了另一个异常:'Date'

[英]During handling of the above exception, another exception occurred: 'Date'

背景 -

我有一个 DataFrame,它有两列(日期和降水),格式如下 -

precipitation_df = pd.DataFrame('2016-09-05', NaN), ('2016-09-06', NaN)

客观的 -
我想删除包含日期范围2016-08-24 - 2017-08-24的 NaN 值的行,但首先想分析哪些日期在此范围内受到影响。 因此,我决定使用此代码创建一个新的 DataFrame,所有 NaN 值都在我感兴趣的范围内 -

start_date = '2016-08-23'
end_date = '2017-08-23'

nan_values_df = (precipitation_df['Date'] > start_date) & (df['Date'] <= end_date)

问题 -
当我运行这段代码时,我得到一个巨大的错误,它引用了“日期”:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

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: 'Date'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-131-146522d4b445> in <module>
      2 end_date = '2017-08-23'
      3 
----> 4 nan_values_df = (precipitation_df['Date'] > start_date) & (df['Date'] <= end_date)

~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

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: 'Date'

我觉得这可能与日期格式有关,但我很困惑,想知道是否有人能指出我正确的方向?

您的指令是指df而不是precipitation_df

但是,为了使您的代码更短、更易读且不易出现此类错误,请将此指令更改为:

nan_values_df = precipitation_df.query('@start_date < Date <= @end_date')

认为您可能有错字:

你写了:

nan_values_df = (precipitation_df['Date'] > start_date) & (df['Date'] <= end_date)

但是您应该引用相同的 DataFrame:

nan_values_df = (precipitation_df['Date'] > start_date) & (precipitation_df['Date'] <= end_date)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM