Cedric Cedric - 1 year ago 103
Python Question

Python: How to develop a between_time similar method when on pandas 0.9.0?

I am stick to pandas 0.9.0 as I'm working under python 2.5, hence I have no between_time method available.

I have a DataFrame of dates and would like to filter all the dates that are between certain hours, e.g. between

for all the dates within the DataFrame

import pandas as pd
import numpy as np
import datetime

dates = pd.date_range(start="08/01/2009",end="08/01/2012",freq="10min")
df = pd.DataFrame(np.random.rand(len(dates), 1)*1500, index=dates, columns=['Power'])

How can I develop a method that provides same functionality as

N.B.: The original problem I am trying to accomplish is under Python: Filter DataFrame in Pandas by hour, day and month grouped by year

Answer Source


try to use:


OLD answer:

I'm not sure that it'll work on Pandas 0.9.0, but it's worth to try it:

df[(df.index.hour >= 8) & (df.index.hour <= 9)]

PS please be aware - it's not the same as between_time as it checks only hours and between_time is able to check time like df.between_time('08:01:15','09:13:28')

Hint: download a source code for a newer version of Pandas and take a look at the definition of indexer_between_time() function in pandas/tseries/index.py - you can clone it for your needs

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download