- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Get the values from the pandas series between a specific time?
The Pandas Series.between_time() method is used to select values between particular times of the day. The between_time() method takes two-time parameters and returns a series object with selected values.
The between_time method is similar to the at_time method of pandas series object, the at_time method selects the values at a particular time whereas The between_time method will select the values between times.
It will raise the TypeError if the index of the input series object is not a DatetimeIndex.
By default, both input time (start_time, end_time) parameters are inclusive, if you want to change that we can use include_start and include_end parameters.
Example 1
import pandas as pd # create the index index = pd.date_range('2021-01-1', periods=10, freq='20T') #creating pandas Series with date time index series = pd.Series([1,2,3,4,5,6,7,8,9,10], index=index) print(series) # selecting values by between_time() print("Selecting values:", series.between_time("00:10","1:40"))
Explanation
Here, we have created a pandas.Series object by using the pandas DateTime index with some list of integer values. After that, we applied the between_time() method to get the values between times “00:10” to “1:40”.
If the specified times are present in the index of the given series object then it will return a new series object with collected values with those DateTime indexes.
Output
2021-01-01 00:00:00 1 2021-01-01 00:20:00 2 2021-01-01 00:40:00 3 2021-01-01 01:00:00 4 2021-01-01 01:20:00 5 2021-01-01 01:40:00 6 2021-01-01 02:00:00 7 2021-01-01 02:20:00 8 2021-01-01 02:40:00 9 2021-01-01 03:00:00 10 Freq: 20T, dtype: int64 Selecting values: 2021-01-01 00:20:00 2 2021-01-01 00:40:00 3 2021-01-01 01:00:00 4 2021-01-01 01:20:00 5 2021-01-01 01:40:00 6 Freq: 20T, dtype: int64
We have collected the values between the “00:10” to “1:40” times. In this example, start_time (“00:10”) and end_time (“1:40”) both are inclusive.
Example 2
import pandas as pd # create the index index = pd.date_range('2021-01-1', periods=10, freq='20T') #creating pandas Series with date time index series = pd.Series([1,2,3,4,5,6,7,8,9,10], index=index) print(series) # selecting values by between_time() print("Selecting values:", series.between_time("00:40","1:40", include_start=False,include_end=False))
Explanation
In the following example, we have applied the between_time() method by not including the start and end times, which is done by providing the False value to the include_start and include_end parameters.
Output
2021-01-01 00:00:00 1 2021-01-01 00:20:00 2 2021-01-01 00:40:00 3 2021-01-01 01:00:00 4 2021-01-01 01:20:00 5 2021-01-01 01:40:00 6 2021-01-01 02:00:00 7 2021-01-01 02:20:00 8 2021-01-01 02:40:00 9 2021-01-01 03:00:00 10 Freq: 20T, dtype: int64 Selecting values: 2021-01-01 01:00:00 4 2021-01-01 01:20:00 5 Freq: 20T, dtype: int64
In this example, we have successfully selected the 2 rows from the given series object, these 3 rows are having the DateTime index time between "00:40", "1:40" both times are not inclusive.
- Related Articles
- Python Pandas - Extract the minute from DateTimeIndex with specific time series frequency
- Python Pandas - Extract year from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the seconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the microseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the nanoseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the timezone from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the day from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract month number from the DateTimeIndex with specific time series frequency
- How to get the index and values of series in Pandas?
- Python Pandas - Extract the day of week from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the quarter of the date from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the ordinal day of year from the DateTimeIndex with specific time series frequency
- How to get the final rows of a time series data using pandas series.last() method?
- How to get few rows from a Series in Pandas?
