Hướng dẫn how do i get the iso week number in python? - làm cách nào để lấy số tuần iso trong python?

Tôi tóm tắt cuộc thảo luận thành hai bước:

  1. Chuyển đổi định dạng thô thành đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    5.
  2. Sử dụng chức năng của đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    5 hoặc đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    7 để tính số tuần.

Ấm lên

from datetime import datetime, date, time
d = date(2005, 7, 14)
t = time(12, 30)
dt = datetime.combine(d, t)
print(dt)

Bước đầu tiên

Để tạo thủ công một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5, chúng ta có thể sử dụng
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
9 hoặc
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
0.

Nhưng trong thực tế, chúng ta thường cần phải phân tích một chuỗi hiện có. Chúng ta có thể sử dụng hàm

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
1, chẳng hạn như
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
2 trong đó bạn phải định dạng cụ thể. Chi tiết của mã định dạng khác nhau có thể được tìm thấy trong tài liệu chính thức.

Ngoài ra, một cách thuận tiện hơn là sử dụng mô -đun DateParse. Ví dụ là

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
3,
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
4 hoặc
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
5

Hai cách tiếp cận trên sẽ trả về một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5.

Bước thứ 2

Sử dụng đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5 thu được để gọi
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
8. Ví dụ,

Python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.

Thật khó để quyết định sử dụng định dạng nào. Một cách tốt hơn là lấy một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
7 để gọi
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
0. Ví dụ,

Python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard

Thật khó để quyết định sử dụng định dạng nào. Một cách tốt hơn là lấy một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
7 để gọi
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
0. Ví dụ,

Hướng dẫn how do i get the iso week number in python? - làm cách nào để lấy số tuần iso trong python?

Hình ảnh của Lukas, pexels.

6 phút để đọc

Trong thương mại điện tử và tiếp thị, nó tương đối phổ biến để sử dụng số tuần ISO khi báo cáo dữ liệu. Hệ thống Tuần lễ ISO là lịch Tuần lễ LEAP là một phần của tiêu chuẩn ngày và thời gian ISO 8601 từ Tổ chức Tiêu chuẩn hóa Quốc tế hoặc ISO.

Lịch Gregorian có một chu kỳ nhảy vọt sử dụng 97 ngày nhảy được phân phối trong khoảng thời gian 400 năm và chứa toàn bộ số 20871 tuần. Điều này có nghĩa là năm trung bình có 52.1775 tuần. Ngược lại, năm tuần ISO hoặc năm ISO, có 52 hoặc 53 tuần đầy đủ, với 364 hoặc 371 ngày, thay vì 365 hoặc 366 thông thường trong một năm lịch Gregorian.

ISO Weeks bắt đầu vào thứ Hai và kết thúc vào Chủ nhật, có xu hướng là cách phổ biến mà các doanh nghiệp đo lường hiệu suất giao dịch của họ. Để truy vấn cơ sở dữ liệu hoặc nền tảng phân tích web để trích xuất dữ liệu thương mại điện tử hoặc tiếp thị được liên kết với ISO Weeks, bạn sẽ cần có thể xác định ngày bắt đầu và ngày kết thúc cho một tuần ISO và diễn ra tuần ISO cho một ngày nhất định. Ở đây, cách làm thế nào để làm điều đó trong Python.

Cài đặt các gói

Mặc dù bạn có thể tính toán dữ liệu tuần ISO theo cách thủ công, nhưng có một gói Python hữu ích được gọi là

Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
3 đơn giản hóa các tính toán. Mở máy tính xách tay Jupyter và nhập lệnh bên dưới để cài đặt gói
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
3.

Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)

Tải các gói

Tiếp theo, nhập gói

Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
5 và mô -đun
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
6 từ
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
3.

import pandas as pd
from isoweek import Week

Nhận số tuần ISO cho một ngày

Đầu tiên, chúng tôi sẽ giải quyết cách tính toán tuần ISO cho một ngày nhất định bằng cách tạo một chức năng chúng tôi có thể sử dụng trong các dự án khác. Chúng tôi sẽ chuyển một chuỗi

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
7 cho điều này và sử dụng chức năng Pandas
Requirement already satisfied: isoweek in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (1.3.3)
9 để chuyển đổi nó thành đối tượng
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5. Sau đó, chúng tôi sẽ sử dụng
import pandas as pd
from isoweek import Week
1 và trả về giá trị thứ hai
import pandas as pd
from isoweek import Week
2 từ danh sách để có được số nguyên chứa số tuần ISO.

def get_iso_week_from_date(date):
    """Return the ISO week from a date.

    Args:
        date (str): Date

    Returns:
        week (int): ISO week
    """
    date = pd.to_datetime(date)
    return date.isocalendar()[1]

week = get_iso_week_from_date('2022-02-28')
week

Nhận ngày bắt đầu cho một số tuần ISO

Tiếp theo, chúng tôi sẽ tạo ra một chức năng Python để tính toán ngày bắt đầu cho một tuần ISO đã cho bằng năm và số tuần ISO. Chúng tôi cũng sẽ thêm một đối số tùy chọn cho

import pandas as pd
from isoweek import Week
3 để bạn có thể trả về ngày dưới dạng chuỗi hoặc đối tượng
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5.

Vì ISO Weeks bắt đầu vào thứ Hai và kết thúc vào Chủ nhật, chúng tôi có thể tính toán ngày bắt đầu cho một tuần ISO nhất định bằng cách chuyển các lập luận

import pandas as pd
from isoweek import Week
5 và
import pandas as pd
from isoweek import Week
6 cho
import pandas as pd
from isoweek import Week
7 và nối thêm
import pandas as pd
from isoweek import Week
8 để trả lại ngày cụ thể đó. Nếu chúng tôi muốn ngày bắt đầu trong tuần ISO được trả về dưới dạng chuỗi ngày, tức là
import pandas as pd
from isoweek import Week
9, thì chúng tôi có thể vượt qua trong kiểu định dạng tùy chọn, chẳng hạn như
def get_iso_week_from_date(date):
    """Return the ISO week from a date.

    Args:
        date (str): Date

    Returns:
        week (int): ISO week
    """
    date = pd.to_datetime(date)
    return date.isocalendar()[1]
0 và chúng tôi sẽ lấy lại ngày ở định dạng đã chọn.

def get_start_date_of_isoweek(year, week, date_format='datetime'):
    """
    Get the start date of the given ISO week using isocalendar()

    Args:
        year (int): Year, i.e. 2022
        week (int): Week, i.e. 34
        date_format (string): Format of the returned date, default is datetime

    Returns:
        datetime.date: Start date of the given ISO week
    """
    
    if date_format == 'datetime':
        return Week(year, week).monday()
    else:
        return Week(year, week).monday().strftime(date_format)

start = get_start_date_of_isoweek(2022, 9)
start

datetime.date(2022, 2, 28)

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
0

Nhận ngày kết thúc cho một số tuần ISO

Quá trình để có được ngày kết thúc trong một tuần ISO là giống nhau, nhưng thay vì thêm

import pandas as pd
from isoweek import Week
8, chúng tôi nối tiếp
def get_iso_week_from_date(date):
    """Return the ISO week from a date.

    Args:
        date (str): Date

    Returns:
        week (int): ISO week
    """
    date = pd.to_datetime(date)
    return date.isocalendar()[1]
2, vì tất cả các tuần ISO kết thúc vào Chủ nhật. Sau đó, chúng ta có thể chạy chức năng để trả về một đối tượng DateTime chứa ngày kết thúc tuần ISO hoặc chuỗi ngày ở định dạng đã chọn của chúng tôi.

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
1

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
2

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
3

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
4

Matt Clarke, Thứ Sáu, ngày 18 tháng 3 năm 2022

Tuần ISO là gì trong Python?

Mô -đun Isoweek cung cấp tuần lớp.Các trường hợp đại diện cho các tuần cụ thể kéo dài từ thứ Hai đến Chủ nhật.Có 52 hoặc 53 tuần được đánh số trong một năm.Tuần 1 được định nghĩa là tuần đầu tiên với 4 ngày trở lên trong tháng Giêng.Nó được gọi là Isoweek vì đây là định nghĩa tuần của ISO 8601.provide the class Week. Instances represent specific weeks spanning Monday to Sunday. There are 52 or 53 numbered weeks in a year. Week 1 is defined to be the first week with 4 or more days in January. It's called isoweek because this is the week definition of ISO 8601.

Tuần ISO là gì?

Lịch Ngày Tuần ISO.Ngày tuần ISO hiện tại là: 2022-W39-3.Đó là tuần 39 của năm 2022, ngày 3 trong tuần và ngày 271 của năm.2022-W39-3. It is week 39 of year 2022, day 3 of the week, and day 271 of the year.