विशिष्ट टाइम सीरीज फ्रीक्वेंसी के साथ DateTimeIndex से महीना नंबर निकालने के लिए, DateTimeIndex.month प्रॉपर्टी का उपयोग करें।
आवश्यक लाइब्रेरी आयात करें
import pandas as pd
DatetimeIndex बनाएं
पीरियड 6 और फ्रीक्वेंसी M (महीना) के साथ DatetimeIndex। टाइमज़ोन Australia/Sydney है:
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='M')
DateTimeIndex प्रदर्शित करें
print("DateTimeIndex...\n", datetimeindex)
महीना नंबर प्राप्त करें (जनवरी=1, फरवरी=2, ..., दिसंबर=12)
print("\nGetting the month number..\n", datetimeindex.month)
पूर्ण उदाहरण
import pandas as pd
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='M')
print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
print("\nGetting the month number..\n", datetimeindex.month)
आउटपुट
DateTimeIndex... DatetimeIndex(['2021-09-30 02:35:55+10:00', '2021-10-31 02:35:55+11:00', '2021-11-30 02:35:55+11:00', '2021-12-31 02:35:55+11:00', '2022-01-31 02:35:55+11:00', '2022-02-28 02:35:55+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='M')
DateTimeIndex frequency...
Getting the month number.. Int64Index([9, 10, 11, 12, 1, 2], dtype='int64')