Pandas DataFrame between_time() method with examples

Spread the love

The between_time() method helps us select values between particular times of the day (e.g., 9:00-9:30 AM).

Syntax –

DataFrame.between_time(start_time, end_time, include_start=True, include_end=True)

start_time – Initial time as a time filter limit.

end_time – End time as a time filter limit.

include_start – Whether the start time needs to be included in the result.

include_end – Whether the end time needs to be included in the result.

Examples –

Let’s create a time series.

import pandas as pd

i = pd.date_range('2018-04-09', periods=4, freq='1D20min')
ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
ts

Now, Let’s select rows between two times.

ts.between_time('0:15', '0:45')
ts.between_time('0:45', '0:15')

Rating: 1 out of 5.

Leave a Reply