Pandas Series.at_time()


Pandas Series.at_time() Tutorial

In this tutorial, we'll explore the Series.at_time() method in Pandas, which is used to select values at a particular time of the day, with well detailed examples.

The syntax of Series.at_time(time) is:

Series.at_time(time)

where

ParameterDescription
timeThe time of day to select.
Parameters of Series.at_time() function

The Series.at_time() method returns a Series containing values at the specified time.


Examples for Series.at_time() function

1. Select values at a specific time using Series.at_time()

In this example, we'll use Series.at_time() to select values at a specific time 12:00:00 of the day.

In the input series, we shall take a range of values starting from 0, whose index is from '2023-01-01' to '2023-01-03' at a frequency of one hour.

Python Program

import pandas as pd

# Create a series with datetime index
date_rng = pd.date_range(start='2023-01-01', end='2023-01-03', freq='H')
series = pd.Series(range(len(date_rng)), index=date_rng)

# Select values at 12:00 PM
selected_values = series.at_time('12:00:00')

# Print original series and selected values
print("Original Series:")
print(series)
print("\nValues at 12:00 PM:")
print(selected_values)

Output

Original Series:
2023-01-01 00:00:00     0
2023-01-01 01:00:00     1
2023-01-01 02:00:00     2
2023-01-01 03:00:00     3
2023-01-01 04:00:00     4
2023-01-01 05:00:00     5
2023-01-01 06:00:00     6
2023-01-01 07:00:00     7
2023-01-01 08:00:00     8
2023-01-01 09:00:00     9
2023-01-01 10:00:00    10
2023-01-01 11:00:00    11
2023-01-01 12:00:00    12
2023-01-01 13:00:00    13
2023-01-01 14:00:00    14
2023-01-01 15:00:00    15
2023-01-01 16:00:00    16
2023-01-01 17:00:00    17
2023-01-01 18:00:00    18
2023-01-01 19:00:00    19
2023-01-01 20:00:00    20
2023-01-01 21:00:00    21
2023-01-01 22:00:00    22
2023-01-01 23:00:00    23
2023-01-02 00:00:00    24
2023-01-02 01:00:00    25
2023-01-02 02:00:00    26
2023-01-02 03:00:00    27
2023-01-02 04:00:00    28
2023-01-02 05:00:00    29
2023-01-02 06:00:00    30
2023-01-02 07:00:00    31
2023-01-02 08:00:00    32
2023-01-02 09:00:00    33
2023-01-02 10:00:00    34
2023-01-02 11:00:00    35
2023-01-02 12:00:00    36
2023-01-02 13:00:00    37
2023-01-02 14:00:00    38
2023-01-02 15:00:00    39
2023-01-02 16:00:00    40
2023-01-02 17:00:00    41
2023-01-02 18:00:00    42
2023-01-02 19:00:00    43
2023-01-02 20:00:00    44
2023-01-02 21:00:00    45
2023-01-02 22:00:00    46
2023-01-02 23:00:00    47
2023-01-03 00:00:00    48
Freq: H, dtype: int64

Values at 12:00 PM:
2023-01-01 12:00:00    12
2023-01-02 12:00:00    36
Freq: 24H, dtype: int64

Summary

In this tutorial, we've covered the Series.at_time() method in Pandas, which is useful for selecting values at a specific time of the day in a Series with a datetime index.


Python Libraries