Python | Panda Timestamp.year

Python Methods and Functions

The Pandas attribute Timestamp.year returns the year that the date in this Timestamp object is.

Syntax: Timestamp.year

Parameters: None

Return: year

Example # 1: Use the Timestamp.year attribute to find the year in which the date present in this Timestamp object is located.

# import pandas as pd

import pandas as pd

 
# Create Timestamp object

ts = pd.Timestamp (year = 2011 , month = 11 , day = 21 ,

hour = 10 , second = 49 , tz = 'US / Central' )

 
# Print the Timestamp object

print (ts)

Output:

Now we will use the attribute Timestamp.year to find the date value s a year.

# return year
ts.year

Output:

As we can see in the output, the Timestamp.year attribute returned 2011 indicating that the year value of the date in the give Timestamp object is 2011.

Example # 2: Use the Timestamp.year attribute to find the year the date is in present in this Timestamp object.

# import pandas as pd

import pandas as pd

 
# Create Timestamp object

ts = pd.Timestamp (year = 2009 , month = 5 , day = 31

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

 
# Print the Timestamp object

print (ts)

Output:

We will now use the Timestamp.year attribute to find the date value in the year.

# return year
ts.year

Output:

As we can see from the output, the Timestamp.year attribute returned 2009, indicating that the value of the year in the date in the object give Timestamp is 2009.





Get Solution for free from DataCamp guru