Python | Pandas Timestamp.hour

Python Methods and Functions

The Pandas attribute Timestamp.hour returns the hour for a given Timestamp.

Syntax: Timestamp .hour

Parameters: None

Return: hour

Example # 1: Use the Timestamp.hour attribute to find out the hour in a given Timestamp object.

# pandas import as pd

import pandas as pd

 
# Create Timestamp object

ts = pd.Timestamp ( 2017 , 2 , 15 , 12 )

 
# Print Timestamp object

print (ts)

Output:

We will now use the Timestamp.hour attribute to find out the hour in a given Timestamp object.

# return hour
ts.hour

Output:

How can we see in output, attribute Timestamp.hour < / code> returned 12 as the hour for the given Timestamp object.

Example # 2: Use the Timestamp.hour attribute to find the hour in the given a Timestamp object.

# import pandas as pd

import pandas as pd

  
# Create Timestamp object

ts = pd.Timestamp (year = 2009 , month = 10 , day = 21 ,

  hour = 4 , tz = 'Europe / Berlin' )

 
# Print the Timestamp object

print (ts)

Output:

We will now use the Timestamp.hour attribute to find out the hour in a given Timestamp object.

# return hour value
ts.hour

Output:

As we can see from the output, the Timestamp.hour attribute returned 4 as the hour value for this Timestamp object.



Get Solution for free from DataCamp guru