Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Pandas - 특정 시계열 빈도로 DateTimeIndex에서 요일 추출

<시간/>

특정 시계열 빈도로 DateTimeIndex에서 요일을 추출하려면 DateTimeIndex.dayofweek를 사용하세요. 재산

먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

기간이 6이고 빈도가 D 즉 day −

인 DatetimeIndex를 만듭니다.
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

DateTimeIndex 빈도 표시 -

print("DateTimeIndex frequency...\n", datetimeindex.freq)

요일을 가져옵니다. 주는 월요일에 시작한다고 가정합니다. 월요일은 0으로 표시되고 일요일은 6 -

로 표시됩니다.
print("\nGet the day of week..\n",datetimeindex.dayofweek)

예시

다음은 코드입니다 -

import pandas as pd

# DatetimeIndex with period 6 and frequency as D i.e. day
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# Get the day of week
# It is assumed the week starts on Monday,
# which is denoted by 0 and ends on Sunday which is denoted by 6
print("\nGet the day of week..\n",datetimeindex.dayofweek)

출력

이것은 다음 코드를 생성합니다 -

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
'2021-10-26 02:30:50+11:00', '2021-10-29 02:30:50+11:00',
'2021-11-01 02:30:50+11:00', '2021-11-04 02:30:50+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='3D')
DateTimeIndex frequency...
<3 * Days>

Get the day of week..
Int64Index([2, 5, 1, 4, 0, 3], dtype='int64')