我有一个以日期时间为索引的数据框。类似于下面的
>>> data.index
Index(['2019-05-15 23:45:00', '2019-05-15 23:46:00', '2019-05-15 23:47:00',
'2019-05-15 23:48:00', '2019-05-15 23:49:00', '2019-05-15 23:50:00',
'2019-05-15 23:52:00', '2019-05-15 23:53:00', '2019-05-15 23:54:00',
'2019-05-15 23:55:00',
...
'2019-05-19 05:36:00', '2019-05-19 05:37:00', '2019-05-19 05:38:00',
'2019-05-19 05:39:00', '2019-05-19 05:40:00', '2019-05-19 05:41:00',
'2019-05-19 05:42:00', '2019-05-19 05:43:00', '2019-05-19 05:44:00',
'2019-05-19 05:45:14'],
dtype='object', name='date', length=989)
我需要选择与日期2019-05-16
相对应的所有行。我尝试了这两种方法。
1)data['2019-05-16']
给出KeyError: '2019-05-16'
2)data.loc['2019-05-16']
给出KeyError: 'the label [2019-05-16] is not in the [index]'
答案 0 :(得分:1)
首先将值转换为DatetimeIndex,因为现在它是datetimes的字符串repr:
data.index = pd.to_datetime(data.index)
data = pd.DataFrame({'a':range(3)},
index=['2019-05-15 23:45:00','2019-05-16 23:46:00','2019-05-17 23:47:00'])
data.index = pd.to_datetime(data.index)
print(data)
a
2019-05-15 23:45:00 0
2019-05-16 23:46:00 1
2019-05-17 23:47:00 2
您的解决方案正在运行,如果存在至少一个带有日期的索引值:
print (data['2019-05-16'])
a
2019-05-16 23:46:00 1
如果不存在,则有其他选择:
print (data[data.index.normalize() == '2019-05-18'])
Empty DataFrame
Columns: [a]
Index: []
print (data[data.index.floor('d') == '2019-05-18'])
Empty DataFrame
Columns: [a]
Index: []
print (data[data.index.date == '2019-05-18'])
Empty DataFrame
Columns: [a]
Index: []