Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
communitycreator

How to obtain the hour of the day component of a period in pandas

Onyejiaku Theophilus Chidalu

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 library
import pandas as pd

# creating a Period object
a = pd.Period('2022-5-2 13:03:12.050000')

# obtaining the hour of the day
b = a.hour

# printing the value
print(f'It is the {b}th hour of the day.')
Implementing the hour attribute

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Period object a.
  • Line 9: We obtain the hour of the year from the Period object using the hour attribute. We assign the result to a variable, b.
  • Line 12: We print the value of b.

RELATED TAGS

pandas
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring