특정 시계열 빈도로 DateTimeIndex에서 연도를 추출하려면 DateTimeIndex.year를 사용하세요. 재산.
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
기간이 6이고 빈도가 Y 즉 년인 DatetimeIndex. 시간대는 호주/시드니입니다 -
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='Y') DateTimeIndex 표시 -
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')