我正试图从熊猫时间序列中获取今天的数据。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10000,1),columns=['A'],
index=pd.date_range('20140518',periods=10000,freq='T'))
Follwing似乎工作正常:
df['2014-05-19']
但是当我使用datetime来使用下面的所有方法来获取今天的日期时
df[datetime.date.today()]
df[datetime.datetime.now()]
df[datetime.datetime.now().date()]
我收到以下Keyerror消息
KeyError: datetime.date(2014, 5, 19)
详细错误堆栈如下所示
C:\Users\<user>\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\series.pyc in __getitem__(self, key)
489 def __getitem__(self, key):
490 try:
--> 491 result = self.index.get_value(self, key)
492 if isinstance(result, np.ndarray):
493 return self._constructor(result,index=[key]*len(result)).__finalize__(self)
C:\Users\<user>\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\tseries\index.pyc in get_value(self, series, key)
1228 return self.get_value_maybe_box(series, key)
1229 except (TypeError, ValueError, KeyError):
-> 1230 raise KeyError(key)
1231
1232 def get_value_maybe_box(self, series, key):
KeyError: datetime.date(2014, 5, 19)
答案 0 :(得分:1)
尝试:
df[df.index.date == datetime.date.today()]