Pandas.DataFrame.Between_Time()
The pandas.DataFrame.between_time() selects the data between particular times of the day that are provided as parameters. It is possible to control the start and end time boundaries by passing the inclusive parameter.
Syntax:
Let’s see the syntax and parameters that are passed to this function:
1. start_time – We need to pass the start time in the datetime.time or string format.
2. end_time – We need to pass the endtime in the datetime.time or string format.
3. inclusive (by default = ‘both’) – This parameter is used to include/exclude the Time boundaries.
- If it is set to “both”, both the start time and end time boundaries are included.
- If it is set to “neither”, both the start time and end time boundaries won’t be included.
- If it is set to “left”, only the start time boundary is included.
- If it is set to “right”, only the end time boundary is included.
Example 1: Including Both Boundaries
1. Create the DateTime from 1st january 2023 for every 15 minutes until 10 periods.
2. Create the “Rainfall” DataFrame with the “Status” column that holds 10 strings and use the DateTime that is created previously as an index to it.
3. Get the “Rainfall” data between 12:00 AM and 1:00 AM by including both the start time and end time boundaries.
# Generate DateTime from 1st January 2023 for every 15 minutes till 10 periods
date_time_range = pandas.date_range('01/01/2023', periods = 10, freq ='15T')
Rainfall = pandas.DataFrame({"Status":["Yes","Yes","No","Yes","No","No","No","Yes","No","Yes"]},
index = date_time_range)
print(Rainfall,"\n")
# inclusive='both'
print(Rainfall.between_time('00:00', '01:00',inclusive='both'))
Output:
The first output represents the original DataFrame. In the second output (highlighted in green), there are five rows that are in between 12:00 AM and 1:00 AM. You will see that both boundaries (highlighted in orange) are included.
Example 2: Excluding Both the Boundaries
1. Create the DateTime from 1st May 2023 for every 60 minutes until 8 periods.
2. Create the “Rainfall” DataFrame with the “Status” column that holds 8 strings and use the DateTime that is created previously as an index to it.
3. Get the “Rainfall” between 04:00 AM and 06:00 AM without the start and end boundaries.
# Generate DateTime from 1st May 2023 for every 60 minutes till 8 periods
date_time_range = pandas.date_range('01/05/2023', periods = 8, freq ='60T')
Rainfall = pandas.DataFrame({"Status":["Yes","Yes","No","Yes","No","No","No","Yes"]},
index = date_time_range)
print(Rainfall,"\n")
# inclusive='neither'
print(Rainfall.between_time('04:00', '06:00',inclusive='neither'))
Output:
The first output represents the original DataFrame. In the second output (highlighted in green), there is only one row between 04:00 AM and 06:00 AM. You will see that both boundaries (2023-01-05 04:00:00 and 2023-01-05 06:00:00) are excluded.
Example 3: Including the Start Time (Left) Boundary
1. Create the DateTime from 5th May 2023 for every 30 minutes until 6 periods.
2. Create the “Temperature” DataFrame with the “Celsius” column that holds 6 values and use the DateTime that is created previously as an index to it.
3. Get the “Temperature” between 12:00 AM and 02:00 AM without including the end boundary.
# Generate DateTime from 5th May 2023 for every 30 minutes till 6 periods
date_time_range = pandas.date_range('05/05/2023', periods = 6, freq ='30T')
Temperature = pandas.DataFrame({"Celsius":[32,43,32,29,30,40]},
index = date_time_range)
print(Temperature,"\n")
# inclusive='left'
print(Temperature.between_time('12:00', '02:00',inclusive='left'))
Output:
The first output represents the original DataFrame. In the second output (highlighted in green), there are four Celsius values between 12:00 AM and 02:00 AM. You will see that only the start time (left boundary highlighted in orange) is included and the end time (right boundary – 2023-05-05 02:00:000 is excluded.
Example 4: Including the End Time (Right) Boundary
Get the “Temperature” between 01:00 AM and 02:00 AM without including the start boundary.
# Generate DateTime from 5th May 2023 for every 30 minutes till 6 periods
date_time_range = pandas.date_range('05/05/2023', periods = 6, freq ='30T')
Temperature = pandas.DataFrame({"Celsius":[32,43,32,29,30,40]},
index = date_time_range)
print(Temperature,"\n")
# inclusive='right'
print(Temperature.between_time('01:00', '02:00',inclusive='right'))
Output:
The first output represents the original DataFrame. In the second output (highlighted in green), there are two Celsius values between 01:00 AM and 02:00 AM. You will see that only the end time (left boundary highlighted in orange) is included and the start time (left boundary – 2023-05-05 01:00:000 is excluded.
Conclusion
We learned how to get the data from the Pandas DataFrame between the given times using the pandas.DataFrame.between_time() function. For a clear understanding, we explained all the parameters as separate examples with real time data including the “Rainfall” and “Temperature”. Carefully look at the “inclusive” parameter, then only you will be able to understand all the examples provided in this guide.