Python | Pandas dataframe.between_time() - GeeksforGeeks

16 Nov, 2018
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.between_time() is used to select values between particular times of the day (e.g. 9:00-9:30 AM). Unlike dataframe.at_time() function, this function extracts values in a range of time. This function is only used with time-series data. The index of the Dataframe must be DatetimeIndex in order to be able to use this function.
Syntax: DataFrame.between_time(start_time, end_time, include_start=True, include_end=True)
Parameters:start_time : datetime.time or stringend_time : datetime.time or stringinclude_start : boolean, default Trueinclude_end : boolean, default True
Returns: values_between_time : type of caller
Note: between_time() function raises exception when the index of the dataframe is not a DatetimeIndex
Example #1: Use between_time() function to find the values between a given time interval.
# importing pandas as pdimport pandas as pd  # Creating row index values for dataframe# Taken time frequency to be of 30 minutes interval# Generating eight index value using "period = 8" parameterind = pd.date_range('01/01/2000', periods = 8, freq ='30T')  # Creating a dataframe with 2 columns# using "ind" as the index for dataframe  df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8],                   "B":[10, 20, 30, 40, 50, 60, 70, 80]},                                             index = ind)  # Printing the dataframedf
Now let’s query for time between “02:00” to “03:30”
# Find the row values between time "02:00" to "03:30"df.between_time('02:00', '03:30')
Output : Example #2: Use between_time() function to find the values between a given time interval while excluding the start and end times.
# importing pandas as pdimport pandas as pd  # Creating row index values for our data frame# Taken time frequency to be of 30 minutes interval# Generating eight index value using "period = 8" parameterind = pd.date_range('01/01/2000', periods = 8, freq ='30T')  # Creating a dataframe with 2 columns# using "ind" as the index for our dataframe  df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8],                   "B":[10, 20, 30, 40, 50, 60, 70, 80]},                                             index = ind)  # query for time between "02:00" to "03:30" with# both the start and end time values being excludeddf.between_time('02:00', '03:30', include_start = False,                                    include_end = False)
Output :
Notice the values corresponding to start time and end time is not included in the dataframe returned by the between_time() function.
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Create a directory in Python
Python | os.path.join() method
Python | Pandas dataframe.groupby()
Python | Get unique values from a list