특정 시계열 빈도로 DateTimeIndex에서 월 번호를 추출하려면 DateTimeIndex.month를 사용하세요. 재산.
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
기간이 6이고 빈도가 M 즉 월인 DatetimeIndex. 시간대는 호주/시드니입니다 -
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='M') DateTimeIndex 표시 -
print("DateTimeIndex...\n", datetimeindex) 월 번호 가져오기, 즉 1월=1, 2월=2, ..., 12월=12 −
print("\nGetting the month number..\n",datetimeindex.month)
예
다음은 코드입니다 -
import pandas as pd
# DatetimeIndex with period 6 and frequency as M i.e. month
# timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='M')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# get the month number i.e. January=1, february=2, ..., December=12
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...
<MonthEnd>
Getting the month number..
Int64Index([9, 10, 11, 12, 1, 2], dtype='int64')