Select Values at Particular Time in DataFrame

To select values at particular time of day, call at_time() method on this DataFrame and pass particular time as parameter to this method.

In this tutorial, we will learn the syntax of DataFrame.agg() and how to use this method to select only those values which has the specified time.

Syntax

The syntax of pandas DataFrame.at_time() method is

</>
Copy
DataFrame.at_time(time, asof=False, axis=None)

where

ParameterValueDescription
timedatetime.time or strParticular time.
axis{0 or ‘index’, 1 or ‘columns’}, default 0

Return Value

DataFrame.

Examples

Select Values at Particular Time in DataFrame

In the following program, we create a DataFrame with a single column col_1 with datetime as index at regular frequency of 12 hours and selecting the values at 12:00 using at_time().

Example.py

</>
Copy
import pandas as pd

i = pd.date_range('2021-04-09', periods=4, freq='6H')
df = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)

result = df.at_time('12:00')
print(result)

Output

                     A
2021-04-09 12:00:00  3

Conclusion

In this Pandas Tutorial, we learned how to select values based on specific time using pandas DataFrame.at_time() method.