विशिष्ट समय श्रृंखला आवृत्ति के साथ DateTimeIndex से वर्ष निकालने के लिए, DateTimeIndex.year का उपयोग करें संपत्ति।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
डेटटाइम इंडेक्स अवधि 6 और आवृत्ति वाई यानी वर्षों के साथ। समय क्षेत्र ऑस्ट्रेलिया/सिडनी है -
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='Y') डेटटाइम इंडेक्स प्रदर्शित करें -
print("DateTimeIndex...\n", datetimeindex) वर्ष प्राप्त करें -
print("\nGetting the year name..\n",datetimeindex.year)
उदाहरण
निम्नलिखित कोड है -
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) आउटपुट
यह निम्नलिखित आउटपुट देगा -
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')