- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 select values from a pandas.series object using the at_time() method?
The Pandas Series.at_time() method is used to select values at a particular time of a given series object. The at_time() method takes a time parameter and returns a series object with selected values.
The at_time method will return an empty Series object if the specified time is not there in the index of the given series object, and it raises the TypeError if the index of the input series object doesn’t have the DatetimeIndex.
Let's create a pandas Series object with Datetime Index and get the values using the Series.at_time() method. If the specified time is present in the index of the given input series object then it will return a series object with those rows values.
import pandas as pd # create the index index = pd.date_range('2021-01-1', periods=10, freq='6H') #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 print("Selecting values:", series.at_time("6:00"))
In this following example, the series is created by using the pandas DateTime index with some list of integer values. After that, we applied the at_time() method to get the values at the time “6:00”.
2021-01-01 00:00:00 1 2021-01-01 06:00:00 2 2021-01-01 12:00:00 3 2021-01-01 18:00:00 4 2021-01-02 00:00:00 5 2021-01-02 06:00:00 6 2021-01-02 12:00:00 7 2021-01-02 18:00:00 8 2021-01-03 00:00:00 9 2021-01-03 06:00:00 10 Freq: 6H, dtype: int64 Selecting values: 2021-01-01 06:00:00 2 2021-01-02 06:00:00 6 2021-01-03 06:00:00 10 Freq: 24H, dtype: int64
In this example, we have successfully selected the 3 rows from the given series object, these 3 rows are having the time “6:00” hours at their index labels.
import pandas as pd # create the index index = pd.date_range('2021-01-1', periods=10, freq='30T') #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 print("Selecting values:", series.at_time("00:10:00"))
In the same way, we have created a pandas object with pandas DateTime index. After that, we try to get the values from the series at the time “00:10:00”.
2021-01-01 00:00:00 1 2021-01-01 00:30:00 2 2021-01-01 01:00:00 3 2021-01-01 01:30:00 4 2021-01-01 02:00:00 5 2021-01-01 02:30:00 6 2021-01-01 03:00:00 7 2021-01-01 03:30:00 8 2021-01-01 04:00:00 9 2021-01-01 04:30:00 10 Freq: 30T, dtype: int64 Selecting values: Series(, Freq: 30T, dtype: int64)
The output of the following example is an empty series object, which is due to the request time not available in the index of the given series object.
Kickstart Your Career
Get certified by completing the courseGet Started