Python Pandas Series.between_time() Method

In this tutorial, we will learn the python pandas Series.between_time() method using this method we can select the values between particular times of the day. It returns Series consisting of specified dates range from the original Series object and it raises TypeError if the index is not a DatetimeIndex.

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

Syntax

Series.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.

Example: Getting the values from the Series between a specific time

Let's create a Series with DatetimeIndex and get the values between a particular time using the Series.between_time() method. Here, in this example, we are getting the all values because we gave the start_time and end_time at '0:00' and '2:00' respectively. See the below example. The Series.between_time() method returns values that are between and including the start_time and end_time.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=3, freq='50T')
series = pd.Series([1, 2, 3], index=Values)
print(series)
print("-----Selecting values---------")
print(series.between_time('0:00','2:00'))


2021-04-01 00:00:00 1
2021-04-01 00:50:00 2
2021-04-01 01:40:00 3
Freq: 50T, dtype: int64
-----Selecting values---------
2021-04-01 00:00:00 1
2021-04-01 00:50:00 2
2021-04-01 01:40:00 3
Freq: 50T, dtype: int64

Example: Getting the values from the Series between a specific time

This example is similar to the previous one, change the periods, frequency and try to get the values between the start_time and end_time by specifying them in the Series.between_time() method.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=4, freq='20T')
series = pd.Series([1, 2, 3, 4], index=Values)
print(series)
print("-----Selecting values---------")
print(series.between_time('0:00','0:40'))


2021-04-01 00:00:00 1
2021-04-01 00:20:00 2
2021-04-01 00:40:00 3
2021-04-01 01:00:00 4
Freq: 20T, dtype: int64
-----Selecting values---------
2021-04-01 00:00:00 1
2021-04-01 00:20:00 2
2021-04-01 00:40:00 3
Freq: 20T, dtype: int64

Example: Set include_start and include_end to False in Series.between_time() method

In Series.between_time() method, by default the include_start and include_end parameter is set True. So when we try to get the values between the particular time, Series.between_time() method includes the start_time and end_time in the output. If we do not want to include start_time and end_time in the result, we can set include_start and include_end parameter to False. See the below example.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=4, freq='20T')
series = pd.Series([1, 2, 3, 4], index=Values)
print(series)
print("-----Selecting values---------")
print(series.between_time('0:00','0:40',include_start=False,include_end=False))


2021-04-01 00:00:00 1
2021-04-01 00:20:00 2
2021-04-01 00:40:00 3
2021-04-01 01:00:00 4
Freq: 20T, dtype: int64
-----Selecting values---------
2021-04-01 00:20:00 2
Freq: 20T, dtype: int64

Example: Series.between_time() method that raises TypeError

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

#importing pandas as pd
import pandas as pd
series = pd.Series([1, 2, 3, 4])
print(series.between_time('0:00','0:50'))


TypeError: Index must be DatetimeIndex

Conclusion

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