반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Google Spreadsheet
- Redshift
- django
- matplotlib
- list
- GIT
- SQL
- PySpark
- array
- hive
- Mac
- Apache
- google apps script
- Google Excel
- Python
- dataframe
- Github
- Java
- PANDAS
- gas
- c#
- Kotlin
- 파이썬
- PostgreSQL
- string
- Excel
- math
- numpy
- Tkinter
Archives
- Today
- Total
달나라 노트
Python datetime : isocalendar, strftime('%U'), strftime('%W') (week number 추출) 본문
Python/Python datetime
Python datetime : isocalendar, strftime('%U'), strftime('%W') (week number 추출)
CosmosProject 2020. 12. 23. 03:04728x90
반응형
import datetime
dt = datetime.date(2020, 12, 25)
print(dt)
iso_data = dt.isocalendar()
print(iso_data)
print(iso_data[0]) # year
print(iso_data[1]) # week number
print(iso_data[2]) # week day number
- Result
2020-12-25
(2020, 52, 5)
2020
52
5
isocalendar는 date or datetime에 대해 해당 날짜의 년도, 주차(week number), 요일(week day)을 포함한 Tuple 데이터를 반환합니다.
따라서 return된 Tuple의 index = 1인 숫자를 보면 week number를 얻을 수 있습니다.
참고로 반환되는 week day에 대한 숫자는 아래와 같습니다.
월요일 = 1
화요일 = 2
수요일 = 3
목요일 = 4
금요일 = 5
토요일 = 6
일요일 = 7
week number는 date format의 숫자에 strftime을 적용하는 방법으로도 얻을 수 있습니다.
총 2가지 방법이 있는데 다음과 같습니다.
date.strftime('%U') # --> 일요일부터 시작하는 기준으로 week number 계산하여 반환
date.strftime('%W') # --> 월요일부터 시작하는 기준으로 week number 계산하여 반환
아래는 strftime을 이용한 예시입니다.
import datetime
dt_today = datetime.date.today()
print(dt_today)
list_weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for i in range(21):
date = dt_today + datetime.timedelta(days=i)
week_number_sun_start = date.strftime('%U')
weekday_number = date.weekday()
weekday_name = list_weekdays[weekday_number]
print('date = {date} / week day = {week_day} --> week number = {week_number}'\
.format(date=date, week_day=weekday_name, week_number=week_number_sun_start))
-- Result
date = 2021-08-12 / week day = Thu --> week number = 32
date = 2021-08-13 / week day = Fri --> week number = 32
date = 2021-08-14 / week day = Sat --> week number = 32
date = 2021-08-15 / week day = Sun --> week number = 33
date = 2021-08-16 / week day = Mon --> week number = 33
date = 2021-08-17 / week day = Tue --> week number = 33
date = 2021-08-18 / week day = Wed --> week number = 33
date = 2021-08-19 / week day = Thu --> week number = 33
date = 2021-08-20 / week day = Fri --> week number = 33
date = 2021-08-21 / week day = Sat --> week number = 33
date = 2021-08-22 / week day = Sun --> week number = 34
date = 2021-08-23 / week day = Mon --> week number = 34
date = 2021-08-24 / week day = Tue --> week number = 34
date = 2021-08-25 / week day = Wed --> week number = 34
date = 2021-08-26 / week day = Thu --> week number = 34
date = 2021-08-27 / week day = Fri --> week number = 34
date = 2021-08-28 / week day = Sat --> week number = 34
date = 2021-08-29 / week day = Sun --> week number = 35
date = 2021-08-30 / week day = Mon --> week number = 35
date = 2021-08-31 / week day = Tue --> week number = 35
strftime('%U')를 사용했으므로 Sun부터 시작해서 Sat까지가 한 주로 판단되어 동일한 week number를 가지는 것을 알 수 있습니다.
import datetime
dt_today = datetime.date.today()
print(dt_today)
list_weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for i in range(21):
date = dt_today + datetime.timedelta(days=i)
week_number_sun_start = date.strftime('%W')
weekday_number = date.weekday()
weekday_name = list_weekdays[weekday_number]
print('date = {date} / week day = {week_day} --> week number = {week_number}'\
.format(date=date, week_day=weekday_name, week_number=week_number_sun_start))
-- Result
date = 2021-08-12 / week day = Thu --> week number = 32
date = 2021-08-13 / week day = Fri --> week number = 32
date = 2021-08-14 / week day = Sat --> week number = 32
date = 2021-08-15 / week day = Sun --> week number = 32
date = 2021-08-16 / week day = Mon --> week number = 33
date = 2021-08-17 / week day = Tue --> week number = 33
date = 2021-08-18 / week day = Wed --> week number = 33
date = 2021-08-19 / week day = Thu --> week number = 33
date = 2021-08-20 / week day = Fri --> week number = 33
date = 2021-08-21 / week day = Sat --> week number = 33
date = 2021-08-22 / week day = Sun --> week number = 33
date = 2021-08-23 / week day = Mon --> week number = 34
date = 2021-08-24 / week day = Tue --> week number = 34
date = 2021-08-25 / week day = Wed --> week number = 34
date = 2021-08-26 / week day = Thu --> week number = 34
date = 2021-08-27 / week day = Fri --> week number = 34
date = 2021-08-28 / week day = Sat --> week number = 34
date = 2021-08-29 / week day = Sun --> week number = 34
date = 2021-08-30 / week day = Mon --> week number = 35
date = 2021-08-31 / week day = Tue --> week number = 35
date = 2021-09-01 / week day = Wed --> week number = 35
strftime('%W')를 사용했으므로 Mon부터 시작해서 Sun까지가 한 주로 판단되어 동일한 week number를 가지는 것을 알 수 있습니다.
728x90
반응형
'Python > Python datetime' 카테고리의 다른 글
Python datetime : utcnow (UTC.Python UTC, 시스템 시간 상관없이 UTC 추출하기. 협정 세계 표준시.) (0) | 2021.10.28 |
---|---|
Python datetime : weekday, isoweekday (week day number 요일번호 추출) (0) | 2021.03.27 |
Python datetime : strftime(시간 날짜 데이터를 텍스트로) & strptime(텍스트를 시간 날짜 데이터로) (0) | 2020.12.23 |
Python datetime : timedelta(시간 또는 날짜의 차이) (0) | 2020.12.23 |
Python datetime : now(현재 날짜와 시간) & today(현재 날짜) (0) | 2020.12.23 |
Comments