
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Extract year from the DateTimeIndex with specific time series frequency
To extract year from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.year property.
At first, import the required libraries −
import pandas as pd
DatetimeIndex with period 6 and frequency as Y i.e. years. The timezone is Australia/Sydney −
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='Y')
Display DateTimeIndex −
print("DateTimeIndex...\n", datetimeindex)
Get the year −
print("\nGetting the year name..\n",datetimeindex.year)
Example
Following is the code −
import pandas as pd # DatetimeIndex with period 6 and frequency as Y i.e. years # timezone is Australia/Sydney datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # get the year print("\nGetting the year name..\n",datetimeindex.year)
Output
This will produce the following output −
DateTimeIndex... DatetimeIndex(['2021-12-31 02:35:55+11:00', '2022-12-31 02:35:55+11:00', '2023-12-31 02:35:55+11:00', '2024-12-31 02:35:55+11:00', '2025-12-31 02:35:55+11:00', '2026-12-31 02:35:55+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='A-DEC') DateTimeIndex frequency... <YearEnd: month=12> Getting the year name.. Int64Index([2021, 2022, 2023, 2024, 2025, 2026], dtype='int64')
- Related Articles
- Python Pandas - Extract the ordinal day of year from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the minute from DateTimeIndex with specific time series frequency
- Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the seconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the microseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the nanoseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the timezone from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the day from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract month number from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the day of week from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the quarter of the date from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the frequency from the DateTimeIndex
- Python Pandas - Extract the frequency object as a string from the DateTimeIndex
- Python Pandas - How to Round the DateTimeIndex with hourly frequency
- Python Pandas - How to Round the DateTimeIndex with minute frequency

Advertisements