Signup/Sign In
PUBLISHED ON: APRIL 26, 2021

Python pandas Series.at_time() Method

In this tutorial, we will discuss and learn the python pandas Series.at_time() method using which we can select values at a particular time of the day. It returns empty Series if the specified time is not there in the index and it raises TypeError, if the index is not a DatetimeIndex.

The below shows the syntax of the Series.at_time() method.

Syntax

Series.at_time(time, asof=False, axis=None)

Parameters

time: It is the datetime.time or str.

axis:{0 or ‘index’, 1 or ‘columns’}, default 0.

Example: Select values using the Series.at_time() method

Let's create a Series of DatatimeIndex and get the values using the Series.at_time() method. If the specified time present in the index, the Series.at_time() method returns those rows. See the below example.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=6, freq='8H')
series = pd.Series([1, 2, 3, 4, 5, 6], index=Values)
print(series)
print("-----Selecting values---------")
print(series.at_time('8:00'))


2021-04-01 00:00:00 1
2021-04-01 08:00:00 2
2021-04-01 16:00:00 3
2021-04-02 00:00:00 4
2021-04-02 08:00:00 5
2021-04-02 16:00:00 6
Freq: 8H, dtype: int64
-----Selecting values---------
2021-04-01 08:00:00 2
2021-04-02 08:00:00 5
Freq: 24H, dtype: int64

Example: Select values using the Series.at_time() method

This example is similar to the previous one change the periods, frequency and gets the values. See the below example.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=4, freq='10T')
series = pd.Series([1, 2, 3, 4], index=Values)
print(series)
print("-----Selecting values---------")
print(series.at_time('00:10:00'))


2021-04-01 00:00:00 1
2021-04-01 00:10:00 2
2021-04-01 00:20:00 3
2021-04-01 00:30:00 4
Freq: 10T, dtype: int64
-----Selecting values---------
2021-04-01 00:10:00 2
Freq: 10T, dtype: int64

Example: The Series.at_time() method returns empty Series

Here, in this example, the Series.at_time() method returns empty Series because the specified time '10:00' is not present in the index. See the below example.

#importing pandas as pd
import pandas as pd
Values = pd.date_range('2021-04-01', periods=4, freq='10T')
series = pd.Series([1, 2, 3, 4], index=Values)
print(series)
print("-----Selecting values---------")
print(series.at_time('10:00'))


2021-04-01 00:00:00 1
2021-04-01 00:10:00 2
2021-04-01 00:20:00 3
2021-04-01 00:30:00 4
Freq: 10T, dtype: int64
-----Selecting values---------
Series([], Freq: 10T, dtype: int64)

Example: The Series.at_time() method raises TypeError

The index of the Series must be a DatetimeIndex otherwise the Series.at_time() method raises TypeError while selecting values. See the below example.

#importing pandas as pd
import pandas as pd
series = pd.Series([1, 2, 3, 4])
print(series.at_time('10:00'))


TypeError: Index must be DatetimeIndex

Conclusion

In this tutorial, we learned the Python pandas Series.at_time() method. We understood the syntax, the parameter of the function and we solved examples by applying Series.at_time() method on the DataFrame to get the values of the specified time.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.