![]() |
VOOZH | about |
The dt.year attribute returns a Numpy array containing the year value of the DateTime Series Object
Output
Day 1 2012
Day 2 2019
Day 3 2008
Day 4 2010
Day 5 2019
dtype: int64
Syntax: Series.dt.year
Parameter : None
Returns: NumPy array with year value
To extract the year value of the DateTime object we use the Series.dt.year attribute of the Pandas library in Python.
Let us understand it better with an example:
In this example, we are using the Series.dt.year attribute of Pandas library to return the year of the datetime in the underlying data of the given Series object.
Output:
Day 1 2012-12-12 12:12:00
Day 2 2012-12-12 13:12:00
Day 3 2012-12-12 14:12:00
Day 4 2012-12-12 15:12:00
Day 5 2012-12-12 16:12:00
dtype: datetime64[ns]
Now we print the year part of these timestamps using dt.year attribute.
Output
Day 1 2012
Day 2 2012
Day 3 2012
Day 4 2012
Day 5 2012
dtype: int64
As we can see in the output, the Series.dt.year attribute has successfully accessed and returned the year of the DateTime in the underlying data of the given series object.