Hướng dẫn how do i find previous weekdays in python? - làm cách nào để tìm các ngày trong tuần trước đó trong python?

Được đưa ra một ngày, nhiệm vụ là viết một chương trình Python để có được ngày làm việc gần đây nhất trước đó kể từ ngày nhất định.

Example:

Đầu vào: test_date = dateTime [2020, 1, 31] test_date = datetime[2020, 1, 31]

Đầu ra: 2020-01-30 00:00:00 2020-01-30 00:00:00

Giải thích: 31 tháng 1 năm 2020, là một ngày thứ sáu, ngày làm việc cuối cùng là thứ Năm, tức là 30 tháng 1. 31 Jan 2020, being a Friday, last business day is thursday, i.e 30 January.

Đầu vào: test_date = dateTime [2020, 2, 3] test_date = datetime[2020, 2, 3]

Đầu ra: 2020-01-31 00:00:00 2020-01-31 00:00:00

Giải thích: 3 tháng 2 năm 2020, là một ngày thứ Hai, ngày làm việc cuối cùng là thứ Sáu, tức là ngày 31 tháng 1. 3 Feb 2020, being a Monday, last business day is friday, i.e 31 January.

Phương pháp 1: Sử dụng Timedelta [] + Day Day []timedelta[] + weekday[]

Trong đó, chúng tôi thực hiện nhiệm vụ trừ 3 trong trường hợp của Thứ Hai, 2 trong trường hợp Chủ nhật và 1 vào tất cả các ngày khác. Timedelta [] thực hiện nhiệm vụ trừ và các tuyên bố có điều kiện kiểm tra trong một ngày trong tuần.

Python3

from datetime import datetime, timedelta

test_date = datetime[

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0____11
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
1____14
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
5

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
8
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2=
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
5
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6==
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
9from0

from1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
22 from4

from5

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6== from9from0

from1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
22 datetime 4

datetime 5 from0

from1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
22
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2

import1= test_date import4 import5=import7

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7datetime, timedelta0
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0datetime, timedelta3

Output:

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00

Phương pháp 2: Sử dụng toán tử Max [] + % + TimedelTa [] & NBSP;max[] + % operator + timedelta[] 

Thực hiện các tác vụ theo cách tương tự, chỉ có sự khác biệt là phương thức để tính toán các thay đổi khác biệt để có được toán tử tối đa [] và %. & NBSP;

Python3

from datetime import datetime, timedelta

test_date = datetime[

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
0____11
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
1____14
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
5

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
8
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2=
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2

import1= test_date import4 import5=import7

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7datetime, timedelta0
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0datetime, timedelta3

Output:

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00

Phương pháp 2: Sử dụng toán tử Max [] + % + TimedelTa [] & NBSP;pd.tseries.offsets.BusinessDay[n] 

Thực hiện các tác vụ theo cách tương tự, chỉ có sự khác biệt là phương thức để tính toán các thay đổi khác biệt để có được toán tử tối đa [] và %. & NBSP;

Python3

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2______8

Phương pháp 3: Sử dụng pd.tseries.offsets.businessday [n] & nbsp;

Trong đó, chúng tôi tạo ra một ngày làm việc bù đắp là 1 ngày và trừ đi từ ngày được khởi tạo. Điều này trả lại ngày làm việc trước đó như mong muốn.

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
8
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
2=
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
2

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
5
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
6==
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
9from0

from1

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
22 from4

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
6
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
7datetime, timedelta0
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
9
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
0datetime, timedelta3

Phương pháp 2: Sử dụng toán tử Max [] + % + TimedelTa [] & NBSP;

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00

Bài Viết Liên Quan

Chủ Đề