- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
- How to Get the values from the pandas series between a specific time?
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
- How to get items from a series object using the get() method?
- How to remove a specified row From the Pandas Series Using Drop() method?
- How to get the final rows of a time series data using pandas series.last() method?
- How to select at the same time from two Tkinter Listbox?
- How to count the valid elements from a series object in Pandas?
- How to select one item at a time from JCheckBox in Java?
- How to access a single value in pandas Series using the .at attribute?
- How to remove a group of elements from a pandas series object?
- Python Pandas - Return a Series containing counts of unique values from Index object
- How to create a series from a list using Pandas?
- How to apply floor division to the pandas series object by another series object?
- How to access a group of elements from pandas Series using the .iloc attribute with slicing object?
- How to append a pandas Series object to another Series in Python?