Pandas DataFrame between_time() Method

In this tutorial, we will learn the Python pandas DataFrame.between_time() method. This method selects values between particular times of the day. By setting start_time to be later than end_time, you can get the times that are not between the two times. It returns the DataFrame and it raises the TypeError if the index is not a DataTimeIndex.

The below shows the syntax of the DataFrame.between_time() method.

Syntax

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

Parameters

start_time: It represents the datetime.time or str that is Initial time as a time filter limit.

end_time: It represents the datetime.time or str that is End time as a time filter limit.

include_start: It represents the bool(True or False), and the default is True. It indicates that whether the start time needs to be included in the result.

include_end: It represents the bool(True or False), and the default is True. It indicates that whether the end time needs to be included in the result.

axis: If it is '0' means ‘index’ and if it is '1' means ‘columns’, and the default value is 0. It determines the range time on index or column value.

Example 1: Getting the values from the DataFrame between a specific time

The below example shows how to get the values of rows by providing a specific time to the DataFrame.between_time() method. Create a datetime indexed it and get the values at any specific time.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='20T')
df = pd.DataFrame({'A': [1, 2, 3],'B': [1, 2, 3]}, index=Values)
print(df)
print("-----Selecting values---------")
print(df.between_time('00:20','1:00'))

Once we run the program we will get the following result.


A B
2021-01-01 00:00:00 1 1
2021-01-01 00:20:00 2 2
2021-01-01 00:40:00 3 3
-----Selecting values---------
A B
2021-01-01 00:20:00 2 2
2021-01-01 00:40:00 3 3

Example 2: Getting the values from the DataFrame between a specific time

The below example is similar to the previous one. Change the period and frequency values and get the values from the DataFrame between a specific time.

import pandas as pd
Values = pd.date_range('2000-01-01', periods=4, freq='1D20min')
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=Values)
print(df)
print("-----Selecting values---------")
print(df.between_time('0:15', '0:45'))

Once we run the program we will get the following result.


A B
2000-01-01 00:00:00 1 1
2000-01-02 00:20:00 2 2
2000-01-03 00:40:00 3 3
2000-01-04 01:00:00 4 4
-----Selecting values---------
A B
2000-01-02 00:20:00 2 2
2000-01-03 00:40:00 3 3

Example 3: The DataFrame.between_time() method raises TypeError

If the index is not a DataTimeIndex, the DataFrame.between_time() method raises TypeError.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=[1,2,3,4])
print("-----Selecting values---------")
print(df.between_time('00:20','1:00'))

Once we run the program we will get the following result.


TypeError: Index must be DatetimeIndex

Example 4: DataFrame.between_time() method that raises TypeError

The below example is similar to the previous one. Check the DataFrame.between_time() method by setting axis=1.

import pandas as pd
Values = pd.date_range('2000-01-01', periods=4, freq='1D20min')
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=Values)
print(df.between_time('0:15', '0:45',axis=1))

Once we run the program we will get the following result.


TypeError: Index must be DatetimeIndex

Example 5: Set include_start and include_end to False

When include_start and include_end are False, we will not get the start time and end time in the result. The below example shows the result.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=4, freq='20T')
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=Values)
print(df)
print("-----Selecting values---------")
print(df.between_time('00:00','1:00',include_start=False,include_end=False))

Once we run the program we will get the following result.


A B
2021-01-01 00:00:00 1 1
2021-01-01 00:20:00 2 2
2021-01-01 00:40:00 3 3
2021-01-01 01:00:00 4 4
-----Selecting values---------
A B
2021-01-01 00:20:00 2 2
2021-01-01 00:40:00 3 3

Conclusion

In this tutorial, we learned the Python pandas DataFrame.between_time() method. We understood the syntax, the parameter of the function and we solved examples by applying DataFrame.between_time() method on the DataFrame to get the values between the specified time.