简体   繁体   English

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

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

Background -背景 -

I have a DataFrame, that has two columns (Date and Precipitation) in the following format -我有一个 DataFrame,它有两列(日期和降水),格式如下 -

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

Objective -客观的 -
I would like to remove the rows containing NaN values for the date range 2016-08-24 - 2017-08-24 , however firstly wanted to analysis what dates were impacted in this range.我想删除包含日期范围2016-08-24 - 2017-08-24的 NaN 值的行,但首先想分析哪些日期在此范围内受到影响。 I therefore decided to create a new DataFrame, using this code, with all the NaN values in the range I am interested in -因此,我决定使用此代码创建一个新的 DataFrame,所有 NaN 值都在我感兴趣的范围内 -

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

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

Issues -问题 -
When I run this code, I get a huge error, which references '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'

I have a feeling it might be something to do with the date format, but I am stumped and wondered if anyone could point me in the right direction?我觉得这可能与日期格式有关,但我很困惑,想知道是否有人能指出我正确的方向?

Your instruction refers to df instead of precipitation_df .您的指令是指df而不是precipitation_df

But to make your code shorter, more readable and less prone to such errors, change this instruction to:但是,为了使您的代码更短、更易读且不易出现此类错误,请将此指令更改为:

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

Think you may have a typo:认为您可能有错字:

You wrote:你写了:

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

But you should be referencing the same DataFrame:但是您应该引用相同的 DataFrame:

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

暂无
暂无

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

相关问题 在处理上述异常的过程中,又发生了一个异常 - During handling of the above exception, another exception occurred 在处理上述异常期间,发生了另一个异常:Python程序 - During handling of the above exception, another exception occurred: Python program KeyError: 1 在处理上述异常的过程中,又发生了一个异常: - KeyError: 1 During handling of the above exception, another exception occurred: 在处理上述异常的过程中,又发生了一个异常:&amp; google sheet not found - During handling of the above exception, another exception occurred: & google sheets not found “在处理上述异常过程中,发生了另一个异常”for循环中的第一个输入 - “ during the handling of above exception, another exception occurred ” for the first input in the for loop 如何解决“在处理上述异常期间,发生了另一个异常:” - How to fix "During handling of the above exception, another exception occurred: " Flask-restful - 在处理上述异常的过程中,发生了另一个异常 - Flask-restful - During handling of the above exception, another exception occurred 关键错误:1 在处理上述异常的过程中,发生了另一个异常 - Key Error: 1 During handling of the above exception, another exception occurred KeyError:在处理上述异常的过程中,发生了另一个异常 - KeyError: During handling of the above exception, another exception occurred 在处理上述异常期间,发生了另一个异常 - During handling of the above exception, another exception occured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM