loc()函数不会读取Datetime date.today来拆分熊猫DataFrame。 (Keyerror)

时间:2020-07-26 10:37:32

标签: python pandas dataframe datetime keyerror

我有一个熊猫数据框,想按日期提取数据。 如果我将loc()函数的日期设为

data_today = data.loc["2020-07-20"]

它将起作用并显示正确的数据。但是,如果我使用日期时间,它将无法正常工作。

import datetime as dt
from datetime import datetime , timedelta

date_today = dt.date.today() - dt.timedelta(days=5)
print(date_today)

data_today = data.loc["date_today"]
data_today

例如,如果我打印date_today,则格式为数据框“ 2020-07-20”的格式。

感谢帮助

这里是完整的代码和错误消息

import datetime as dt
from datetime import datetime , timedelta

data = pd.read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv" , index_col = "date")
data.head()


date_today = dt.date.today() - dt.timedelta(days=5)
print(date_today)

data_today = data.loc["date_today"]
data_today

和错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\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\index.pyx in pandas._libs.index.IndexEngine._get_loc_duplicates()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine._maybe_get_bool_indexer()

KeyError: 'date_today'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-179-6abf305e8190> in <module>
      5 print(date_today)
      6 
----> 7 data_today = data.loc["date_today"]
      8 data_today

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1765 
   1766             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1767             return self._getitem_axis(maybe_callable, axis=axis)
   1768 
   1769     def _is_scalar_access(self, key: Tuple):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1962         # fall thru to straight lookup
   1963         self._validate_key(key, axis)
-> 1964         return self._get_label(key, axis=axis)
   1965 
   1966 

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
    622             raise IndexingError("no slices here, handle elsewhere")
    623 
--> 624         return self.obj._xs(label, axis=axis)
    625 
    626     def _get_loc(self, key: int, axis: int):

~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
   3535             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3536         else:
-> 3537             loc = self.index.get_loc(key)
   3538 
   3539             if isinstance(loc, np.ndarray):

~\anaconda3\lib\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\index.pyx in pandas._libs.index.IndexEngine._get_loc_duplicates()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine._maybe_get_bool_indexer()

KeyError: 'date_today'

2 个答案:

答案 0 :(得分:1)

您的代码基本上没问题。您需要将索引转换为日期,并且还使用date_today作为变量而不是字符串:

from datetime import datetime , timedelta

data = pd.read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv" , index_col = "date")
data.index = pd.to_datetime(data.index)

date_today = dt.date.today() - dt.timedelta(days=5)
data_today = data.loc[date_today]

print(data_today.head())

输出为:

           iso_code continent     location  total_cases  new_cases  total_deaths  new_deaths  total_cases_per_million  new_cases_per_million  total_deaths_per_million  ...  aged_70_older  \
date                                                                                                                                                                    ...                  
2020-07-21      AFG      Asia  Afghanistan      35615.0      140.0        1186.0         5.0                  914.886                  3.596                    30.466  ...          1.337   
2020-07-21      ALB    Europe      Albania       4171.0       81.0         113.0         1.0                 1449.371                 28.147                    39.266  ...          8.643   
...

答案 1 :(得分:0)

您的代码应该可以正常工作,您只需要正确设置date_today的格式即可。

data.loc[date_today.strftime("%Y-%m-%d")]