我有一个充满了一些数据的hf5。
当我在python中打开它时,我有这个输出
hf['SB'].head()
0_1 price cng
2015-07-15 07:30:00.087 12.61 4
2015-07-15 07:30:00.087 12.61 1
2015-07-15 07:30:00.087 12.61 1
2015-07-15 07:30:00.087 12.61 2
2015-07-15 07:30:00.087 12.61 19
此文件的范围从2015年到2016年。
hf['SB'].index
DatetimeIndex(['2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
'2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
'2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
'2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
'2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
...
'2016-07-14 16:59:57.670000', '2016-07-14 16:59:58.047000',
'2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
'2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
'2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
'2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.957000'],
dtype='datetime64[ns]', name=u'0_1', length=3961015, freq=None)
嗯......我的问题是:
当我需要切片时,例如,2015年8月20日:
hf['SB'][datetime(2015,8,20)]
我收到此错误:KeyError: datetime.datetime(2015, 8, 20, 0, 0)
但如果我使用:
hf['SB']['2015-08-20']
它的作品!!!
我的索引文件中有错误或我使用datetime
函数错误了吗?
答案 0 :(得分:1)
我相信,你正在努力获得索引中根本不存在的密钥的价值。 hf['SB']['2015-08-20']
将为您提供该特定日期的所有记录。
见下面的例子:
>>> rng = pd.date_range('1/1/2016', periods=10, freq='S')
>>> ts = pd.Series(np.random.randn(len(rng)), index=rng)
>>> ts = ts[1:]
>>> ts
2016-01-01 00:00:01 0.133551
2016-01-01 00:00:02 1.067772
2016-01-01 00:00:03 0.591676
2016-01-01 00:00:04 -2.445586
2016-01-01 00:00:05 0.700155
2016-01-01 00:00:06 -0.127861
2016-01-01 00:00:07 1.116494
2016-01-01 00:00:08 -0.427959
2016-01-01 00:00:09 2.115352
Freq: S, dtype: float64
>>> ts[datetime.date(2016,1,1)]
....
KeyError: datetime.date(2016, 1, 1)
>>> ts['2016-01-01']
2016-01-01 00:00:01 0.133551
2016-01-01 00:00:02 1.067772
2016-01-01 00:00:03 0.591676
2016-01-01 00:00:04 -2.445586
2016-01-01 00:00:05 0.700155
2016-01-01 00:00:06 -0.127861
2016-01-01 00:00:07 1.116494
2016-01-01 00:00:08 -0.427959
2016-01-01 00:00:09 2.115352
Freq: S, dtype: float64
因此,使用hf['SB']['2015-08-20']
,您将获得“2015-08-20'”的所有记录,但datetime.datetime(2015,8,20,0,0)
如果您想要使用日期时间切片,请尝试以下操作:
>>> ts[datetime.datetime(2016,1,1,0,0,1):datetime.datetime(2016,1,1,0,0,3)]
2016-01-01 00:00:01 0.133551
2016-01-01 00:00:02 1.067772
2016-01-01 00:00:03 0.591676
Freq: S, dtype: float64