특정 시계열 빈도로 DateTimeIndex에서 날짜의 분기를 추출하려면 DateTimeIndex.quarter를 사용하세요. .
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
기간이 6이고 빈도가 M 즉 월인 DatetimeIndex를 만듭니다. 시간대는 호주/시드니입니다 -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')
DateTimeIndex 빈도 표시 -
print("DateTimeIndex frequency...\n", datetimeindex.freq)
날짜의 분기 가져오기 -
print("\nGet the quarter of the date..\n",datetimeindex.quarter)
결과는 다음 분기를 기준으로 합니다. -
Quarter 1 = 1st January to 31st March Quarter 2 = 1st April to 30th June Quarter 3 = 1st July to 30th September Quarter 4 = 1st October to 31st December
예시
다음은 코드입니다 -
import pandas as pd # DatetimeIndex with period 6 and frequency as M i.e. Month # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # Get the quarter of the date # Result is based on the following quarters of an year: # Quarter 1 = 1st January to 31st March # Quarter 2 = 1st April to 30th June # Quarter 3 = 1st July to 30th September # Quarter 4 = 1st October to 31st December print("\nGet the quarter of the date..\n",datetimeindex.quarter)
출력
이것은 다음 코드를 생성합니다 -
DateTimeIndex... DatetimeIndex(['2021-10-31 02:30:50+11:00', '2021-12-31 02:30:50+11:00', '2022-02-28 02:30:50+11:00', '2022-04-30 02:30:50+10:00', '2022-06-30 02:30:50+10:00', '2022-08-31 02:30:50+10:00'], dtype='datetime64[ns, Australia/Sydney]', freq='2M') DateTimeIndex frequency... <2 * MonthEnds> Get the quarter of the date.. Int64Index([4, 4, 1, 2, 2, 3], dtype='int64')