简体   繁体   中英

How selected time period in a pandas dataframe?

As explain in that Q&A : python pandas dataframe slicing by date conditions I want to select periods of time in a pandas dataframe. The answer given works perfectly for a day-slicing, but won't work if you wan't to look at the hours only.

Here's an example of what I am want to do :

2013-12-12 10:51:51
2013-12-12 11:11:01
2013-12-12 11:19:22
2013-12-12 11:36:48
2013-12-12 11:36:48

hour_frame(df, 11,00,00,11,30,00) # I want to select items between 11h00 and 11h30

2013-12-12 11:11:01
2013-12-12 11:19:22

I tried to use the code given in the answer (cf. the link above)

def hour_frame(df,start_hour,start_minute,end_hour,end_minute):

    start_time = pd.Timestamp('%d:%d:%d' % (start_hour, start_minute, 0)).strftime('%Y-%m-%d %H:%M:%S')
    end_time = pd.Timestamp('%d:%d:%d' % (end_hour, end_minute, 0)).strftime('%Y-%m-%d %H:%M:%S')

    return df.ix[start_time:end_time]

But It return an empty dataframe, I looked at the values of start_time and end_time and they were :

start_time = 2014-07-09 11:00:00
end_time =   2014-07-09 11:30:00

So my problem is that when I create the strings, it will fill automatically the date with the current day, and I don't know how compare the date only looking at the hours.

may be you can do something like:

# create a new column with only time from your date column
df['time'] = df['date'].apply(lambda x: x.time())

#filter based on the time column
mask = (df['time'] > datetime.time(11,00)) & (df['time'] < datetime.time(11,30))
df = df[mask]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM