Hướng dẫn what is iso week in python? - tuần iso trong python là gì?

Hướng dẫn what is iso week in python? - tuần iso trong python là gì?

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 gọi là

import pandas as pd
from isoweek import Week
2 đơ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
import pandas as pd
from isoweek import Week
2.

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

import pandas as pd
from isoweek import Week
4 và mô -đun
import pandas as pd
from isoweek import Week
5 từ
import pandas as pd
from isoweek import Week
2.

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

import pandas as pd
from isoweek import Week
7 cho điều này và sử dụng chức năng Pandas
import pandas as pd
from isoweek import Week
8 để chuyển đổi nó thành đối tượng
import pandas as pd
from isoweek import Week
9. Sau đó, chúng tôi sẽ sử dụng
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à trả về giá trị thứ hai
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]
1 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

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 để bạn có thể trả về ngày dưới dạng chuỗi hoặc đối tượng
import pandas as pd
from isoweek import Week
9.

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 đã cho bằng cách chuyển các lập luận

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]
4 và
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]
5 cho
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]
6 và thêm
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]
7 để 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à
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]
8, 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]
9 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)

end = get_start_date_of_isoweek(2022, 9, '%Y-%m-%d')
end

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 rất giống nhau, nhưng thay vì 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]
7, chúng tôi nối tiếp
week = get_iso_week_from_date('2022-02-28')
week
1, 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.

def get_end_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).sunday()
    else:
        return Week(year, week).sunday().strftime(date_format)

end = get_end_date_of_isoweek(2022, 9)
end

import pandas as pd
from isoweek import Week
0

import pandas as pd
from isoweek import Week
1

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

Tuần ISO là gì?

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.

Tuần ISO được tính toán như thế nào?

Ngày lịch ISO bao gồm ba phần: năm (gần giống như năm Gregorian), số tuần (1-53) và ngày trong vòng một tuần (1-7).Tuần đầu tiên của một năm là tuần của Thứ Năm đầu tiên. Tuần trước là tuần ngay trước tuần đầu tiên của năm tiếp theo. Tuần lễ bắt đầu vào thứ Hai.The first week of a year is the week of the first Thursday. The last week is the week immediately before the first week of the next year. Every week starts on Monday.

Làm thế nào để tôi có được số tuần ISO trong Python?

Cách dễ nhất để có được số tuần là sử dụng hàm python datetime isocalendar ().Hàm isocalendar () trả về một đối tượng tuple với ba thành phần: năm, tuần và ngày trong tuần.Lịch ISO là một biến thể được sử dụng rộng rãi của lịch Gregorian.use the Python datetime isocalendar() function. The isocalendar() function returns a tuple object with three components: year, week and weekday. The ISO calendar is a widely used variant of the Gregorian calendar.

Lịch ISO Python là gì?

Phương thức isocalendar () của lớp DateTime trong python hàm isocalendar () được sử dụng để trả về một bộ giảm của năm ISO, số tuần ISO và ngày trong tuần ISO.Lưu ý: Theo ISO Standard 8601 và ISO Standard 2015, thứ năm là ngày giữa của một tuần.Do đó, ISO năm luôn bắt đầu với thứ Hai.used to return a tuple of ISO Year, ISO Week Number, and ISO Weekday. Note: According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week. Therefore, ISO years always start with Monday.