Python: How to develop a between_time similar method when on pandas 0.9.0?
<p>I am stick to pandas 0.9.0 as I'm working under python 2.5, hence I have no <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.between_time.html" rel="nofollow">between_time</a> method available. </p>

<p>I have a DataFrame of dates and would like to filter all the dates that are between certain hours, e.g. between <code>08:00</code> and <code>09:00</code> for all the dates within the DataFrame <code>df</code>.</p>

<pre><code>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'])
</code></pre>

<p>How can I develop a method that provides same functionality as <code>between_time</code> method?</p>

<p>N.B.: The original problem I am trying to accomplish is under <a href="http://stackoverflow.com/questions/40117702/python-filter-dataframe-in-pandas-by-hour-day-and-month-grouped-by-year">Python: Filter DataFrame in Pandas by hour, day and month grouped by year</a></p>

<p>Here is a NumPy-based way of doing it:</p>

<pre><code>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'])

epoch = np.datetime64('1970-01-01')
start = np.datetime64('1970-01-01 08:00:00')
end = np.datetime64('1970-01-01 09:00:00')

# convert the dates to a NumPy datetime64 array
date_array = df.index.asi8.astype('&lt;M8[ns]') 

# replace the year/month/day with 1970-01-01
truncated = (date_array - date_array.astype('M8[D]')) + epoch

# compare the hour/minute/seconds etc with `start` and `end`
mask = (start &lt;= truncated) &amp; (truncated &lt;=end)

print(df[mask])
</code></pre>

<p>yields</p>

<pre><code>                           Power
2009-08-01 08:00:00  1007.289466
2009-08-01 08:10:00   770.732422
2009-08-01 08:20:00   617.388909
2009-08-01 08:30:00  1348.384210
...
2012-07-31 08:30:00   999.133350
2012-07-31 08:40:00  1451.500408
2012-07-31 08:50:00  1161.003167
2012-07-31 09:00:00   670.545371
</code></pre>

python pandas python-2.5