How to obtain the hour of the day component of a period in pandas
Overview
The hour attribute of a Period object in pandas is used to return the hour of the day component of the Period object.
Syntax
The hour attribute takes the syntax below:
Period.hour
Syntax for the hour attribute
Parameter value
The hour attribute takes no parameter value.
Return value
The hour attribute returns an int between 0 and 23 which indicates the hour of the day.
Example
# A code to illustrate the hour attribute# importing the pandas libraryimport pandas as pd# creating a Period objecta = pd.Period('2022-5-2 13:03:12.050000')# obtaining the hour of the dayb = a.hour# printing the valueprint(f'It is the {b}th hour of the day.')
Explanation
- Line 3: We import the
pandaslibrary. - Line 6: We create a
Periodobjecta. - Line 9: We obtain the hour of the year from the
Periodobject using thehourattribute. We assign the result to a variable,b. - Line 12: We print the value of
b.