在python pandas中用日期范围索引一些行

时间:2018-09-12 11:20:22

标签: python pandas datetime indexing

我有一个按日期(直到秒的年和时间)索引的数据框,我想获取与我计算出的特定时间段相对应的行。但是,用datetime类型或str类型建立索引时,我无法摆脱keyerror。代码如下:

def get_series(self, id_dataset, time=None):
    if time == None:
        time = dt.datetime.now()
    time = time.replace(second=0, microsecond=0)

    dataframe_global = pandas.read_csv('../rsc/datasets/' + str(id_dataset).replace(":", "_") + '.csv',
                                       delimiter=',')

    dataframe_global['timestamp_index'] = pandas.to_datetime(dataframe_global['timestamp'])
    dataframe_global = dataframe_global.set_index('timestamp_index')
    # dataframe_global = pandas.read_csv('../rsc/datasets/historic4.csv', delimiter=',')

    previous_measures = []
    for diff in self.difference_times:
        time1 = last_time - dt.timedelta(minutes=(diff - 30))
        time2 = last_time - dt.timedelta(minutes=(diff + 30))

        ind = str(time2.isoformat(sep=' '))
        ind2 = str(time1.isoformat(sep=' '))
        prev = dataframe_global[ind:ind2]

Keyerror出现在代码的最后一行。

值:

last_time = datetime(2018-07-04 16:55:59.02000)

时间=日期时间(2018-07-04 17:09:59.02000)

time2 = datetime(2018-07-04 16:39:59.02000)

ind ='2018-07-04 16:39:59.02000'

in2 ='2018-07-04 17:09:59.02000'

有任何提示吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的索引器可能定义不正确。我们不确定,因为您没有包含last_timeind / ind2值。

请记住,如果仅从datetime来构建Timestamp索引,则将使用今天的日期,因为datetime对象必须同时指定日期和时间。下面是一个演示。直接使用ind = str(time2)ind2 = str(time1)可能就足够了。

df = pd.DataFrame([[0, 1, 2], [3, 4, 5], [6, 7, 8]],
                  index=['00:00:00', '20:10:20', '12:15:00'])

df.index = pd.to_datetime(df.index)

today = pd.to_datetime('today').date()
start_time = '10:00:00'
end_time = '22:00:00'

res = df[f'{today} {start_time}': f'{today} {end_time}']

print(res)

                     0  1  2
2018-09-12 20:10:20  3  4  5
2018-09-12 12:15:00  6  7  8