Search code examples
pythonpandaspython-2.5

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 08:00 and 09:00 for all the dates within the DataFrame df.

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 between_time method?

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


Solution

  • UPDATE:

    try to use:

    df.loc[df.index.indexer_between_time('08:00','09:50')]
    

    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


    UPDATE: starting from Pandas 0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers.