Pandas filtering - between_time on a column without index

I need to filter data with specific hours. The DataFrame between_time function is apparently the right way to do this, however it only works with the index column in the dataframe; but I need to have the data in the original format (for example, pivot tables will expect the datetime column to have its own name, and not as an index).

This means that each filter looks something like this:

df.set_index(keys='my_datetime_field').between_time('8:00','21:00').reset_index()

This means that every time you run such a filter, two reindexing operations are performed.

Is this good practice or is there a better way to do the same?

+4
source share
1 answer

DatetimeIndex, , DataFrame. indexer_between_time. , df iloc:

import pandas as pd
import numpy as np

N = 100
df = pd.DataFrame(
    {'date': pd.date_range('2000-1-1', periods=N, freq='H'),
     'value': np.random.random(N)})

index = pd.DatetimeIndex(df['date'])
df.iloc[index.indexer_between_time('8:00','21:00')]
+9

All Articles