+

Python | Pandas dataframe.at_time ()

The Pandas function dataframe.at_time() is used to select all values ​​in a row that correspond to the time of day. If there is no input time in the data frame, an empty data frame is returned.

Syntax: DataFrame.at_time (time, asof = False)

Parameters:
time: datetime.time or string

Returns: values_at_time: type of caller

Note: at_time () throws an exception when the dataframe index is not DatetimeIndex

Example # 1: Create an indexed datetime data frame and extract the values ​​at any specific time

# import pandas as pd

import pandas as pd

 
# Create row index values ​​for the data frame
# Accepted frequency at 12 hour intervals

 
# Generate five index values ​​using the period parameter = 5

ind = pd.date_range ( `01/01/2000` , periods = 5 , freq = ` 12H` )

 
# Create a data frame with 2 columns
# using & quot; ind & quot; as an index for our dataframe

 

df = pd.DataFrame ({ "A" : [ 1 , 2 , 3 , 4 , 5 ],

"B" : [ 10 , 20 , 30 , 40 , 50 ]},

  index = ind)

 
# Print data frame
# for rendering
df

Now check the values ​​at 12:00

df.at_time ( `12:00` )

Output:

Example # 2: Set the frequency of the date_time index to a 30 minute duration and query for both valid and invalid time (no in the data frame).

# import pandas as pd

import pandas as pd

 
# Create row index values ​​for our data frame
# We took the time frequency at 30 minute intervals
# We generate eight index values ​​using the "period = 8" parameter

 

ind = pd.date_range ( ` 01/01 / 2000` , periods = 8 , freq = `30T` )

 
# Create a data frame with 2 columns
# using & quot; ind & quot; as an index to our dataframe

df = pd.DataFrame ({ " A " : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ],

"B" : [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 ]},

index = ind)

 
# Print data frame
df

Now let`s ask for the time “02: 00 "

# Find string values ​​at 02:00

df.at_time ( `02: 00` )

Output:

Get Solution for free from DataCamp guru