Signup/Sign In
PUBLISHED ON: MARCH 12, 2021

Pandas DataFrame at_time() Method

In this tutorial, we will learn the Python pandas DataFrame.at_time() method. It is used to select values of rows at a particular time of day. If the specified input time is not present in the DataFrame, it returns the empty DataFrame. It raises the TypeError if the index is not a DataTimeIndex.

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

Syntax

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

Parameters:

time: It represents the datetime.time or str

axis: If it is '0' means ‘index’ and if it is '1' means ‘columns’, and the default value is 0.

Example 1: Getting the values from the DataFrame at a specific time using the DataFrame.at_time() Method

The below example shows how to get the values of rows by providing a specific time to the DataFrame.at_time() method. Create a datetime indexed dataframe and get the values at any specific time.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=4, freq='12H')
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=Values)
print(df)
print("-----Selecting values---------")
print(df.at_time('12:00'))

Once we run the program we will get the following result.


A B
2021-01-01 00:00:00 1 1
2021-01-01 12:00:00 2 2
2021-01-02 00:00:00 3 3
2021-01-02 12:00:00 4 4
-----Selecting values---------
A B
2021-01-01 12:00:00 2 2
2021-01-02 12:00:00 4 4

Example 2: Getting the values from the DataFrame at a specific time using the DataFrame.at_time() Method

The below example is similar to the previous one. Change the period and frequency values and get the values from the DataFrame of a specific time.

import pandas as pd
Values = pd.date_range('2020-02-01', periods=5, freq='20T')
df = pd.DataFrame({'A': [1, 2, 3, 4,5],'B': [1, 2, 3, 4,5]}, index=Values)
print(df)
print("-----Selecting values---------")
print(df.at_time('1:00'))

Once we run the program we will get the following result.


A B
2020-02-01 00:00:00 1 1
2020-02-01 00:20:00 2 2
2020-02-01 00:40:00 3 3
2020-02-01 01:00:00 4 4
2020-02-01 01:20:00 5 5
-----Selecting values---------
A B
2020-02-01 01:00:00 4 4

Example 3: The DataFrame.at_time() method returns empty DataFrame.

When we try to get the values of rows from the DataFrame of a specific time. If the specified input time is not present in the DataFrame, it returns the empty DataFrame.

import pandas as pd
Values = pd.date_range('2020-02-01', periods=5, freq='20T')
df = pd.DataFrame({'A': [1, 2, 3, 4,5],'B': [1, 2, 3, 4,5]}, index=Values)
print("-----Selecting values---------")
print(df.at_time('1:30'))

Once we run the program we will get the following result.


-----Selecting values---------
Empty DataFrame
Columns: [A, B]
Index: []

Example 4: The DataFrame.at_time() method raises TypeError.

If the index is not a DataTimeIndex the DataFrame.at_time() method raises TypeError.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4],'B': [1, 2, 3, 4]}, index=[1,2,3,4])
print("-----Selecting values---------")
print(df.at_time('12:00'))

Once we run the program we will get the following result.


TypeError: Index must be DatetimeIndex

Conclusion

In this tutorial, we learned the Python pandas DataFrame.at_time() method. We understood the syntax, the parameter of the function and we solved examples by applying DataFrame.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.