Đượ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:000____11
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:001____14
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:005
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:008
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:001
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:002
=
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:005
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
=
=
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:009
from
0from
1
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:0022
from
4from
5
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
=
=
from
9from
0from
1
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:0022
datetime
4datetime
5 from
0
from
1
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:0022
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
import
1=
test_date
import
4 import
5=
import
7
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
datetime, timedelta
0 The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
datetime, timedelta
3Output:
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:000____11
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:001____14
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:005
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:008
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:001
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:002
=
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
import
1=
test_date
import
4 import
5=
import
7
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
datetime, timedelta
0 The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
datetime, timedelta
3Output:
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:002______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:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:008
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:001
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:002
=
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:002
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:005
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:006
=
=
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:009
from
0from
1
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:0022
from
4The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:006
The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:007
datetime, timedelta
0 The original date is : 2020-01-31 00:00:00 Last business day : 2020-01-30 00:00:009
The original date is : 2020-02-03 00:00:00 Last business day : 2020-01-31 00:00:000
datetime, timedelta
3Phươ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