【发布时间】:2016-09-01 04:45:51
【问题描述】:
我有一个充满一些数据的 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 函数错误?
【问题讨论】:
-
尝试将
datetime(2015,8,20)转换为字符串,例如hf['SB']['{}'.format(datetime(2015,8,20)] -
Same error: KeyError: '2015-08-20 00:00:00' 我认为这个错误是因为在转换为字符串时需要精确的小时和分钟,例如 00:00:00 .由于我的数据没有这个确切的值,我得到了这个错误
-
试试
hf['SB']['{}'.format(datetime(2015,8,20).date()] -
嘿,它的工作... =] 但我不明白为什么简单的“日期时间(2015,8,20)”不起作用。去年我使用了同样的想法并且工作得很好。也许功能改变了......嗯,谢谢你的建议