Tôi tin rằng không có câu trả lời nào hiện tại vẫn còn hiệu lực trong Python 3, vì vậy vì đây là lần truy cập hàng đầu trong Google cho ngày đầu tiên và cuối cùng của quý, tôi sẽ cung cấp một giải pháp hoạt động trong Python 3 (chủ yếu là AHMET với // thay vì thay vì /):
from datetime import date as date_class from datetime import timedelta, datetime def get_quarter(p_date: date_class) -> int: return (p_date.month - 1) // 3 + 1 def get_first_day_of_the_quarter(p_date: date_class): return datetime(p_date.year, 3 * ((p_date.month - 1) // 3) + 1, 1) def get_last_day_of_the_quarter(p_date: date_class): quarter = get_quarter(p_date) return datetime(p_date.year + 3 * quarter // 12, 3 * quarter % 12 + 1, 1) + timedelta(days=-1) assert get_quarter(datetime(year=2021, month=10, day=5).date()) == 4 assert get_quarter(datetime(year=2020, month=9, day=25).date()) == 3 assert get_quarter(datetime(year=2020, month=12, day=11).date()) == 4 assert get_quarter(datetime(year=2020, month=1, day=2).date()) == 1 assert get_first_day_of_the_quarter(datetime(2020, 10, 5).date()) == datetime(2020, 10, 1) assert get_first_day_of_the_quarter(datetime(2020, 9, 25).date()) == datetime(2020, 7, 1) assert get_first_day_of_the_quarter(datetime(2020, 12, 11).date()) == datetime(2020, 10, 1) assert get_first_day_of_the_quarter(datetime(2020, 1, 2).date()) == datetime(2020, 1, 1) assert get_last_day_of_the_quarter(datetime(2020, 10, 5).date()) == datetime(2020, 12, 31) assert get_last_day_of_the_quarter(datetime(2020, 9, 25).date()) == datetime(2020, 9, 30) assert get_last_day_of_the_quarter(datetime(2020, 12, 11).date()) == datetime(2020, 12, 31) assert get_last_day_of_the_quarter(datetime(2020, 1, 2).date()) == datetime(2020, 3, 31) assert get_last_day_of_the_quarter(datetime(2020, 5, 6).date()) == datetime(2020, 6, 30)Tổng quan
Thuộc tính ____23 của đối tượng Pandas.Timestamp, tương đương với đối tượng DateTime của Python, được sử dụng để kiểm tra xem ngày có phải là ngày đầu tiên của quý hay không.
Thuộc tính
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
4 có cú pháp sau:Cú pháp
Thuộc tính quarter có cú pháp sau:
Timestamp.quarter
Cú pháp cho thuộc tính quý
Giá trị tham số
Vì nó là một thuộc tính, quarter không có giá trị tham số.
Giá trị trả về
Thuộc tính quarter trả về một
Timestamp.quarter
4 từ 1 đến 4 đại diện cho quý mà đối tượng đầu vàoTimestamp.quarter
7 có thể được tìm thấy.Thí dụ
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
Thực hiện thuộc tính quý
Giải trình
- Dòng 4: Chúng tôi nhập thư viện
Timestamp.quarter
8. - Dòng 7: Chúng tôi tạo một đối tượng datetime,
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
0. - Dòng 10: Chúng tôi có được một phần tư của năm từ đối tượng đầu vào datetime bằng thuộc tính quarter.
Người đóng góp
Tổng quan
Thuộc tính ____23 của đối tượng Pandas.Timestamp, tương đương với đối tượng DateTime của Python, được sử dụng để kiểm tra xem ngày có phải là ngày đầu tiên của quý hay không. attribute of the Pandas.Timestamp object, which is an equivalent of Python's datetime object, is used to check if the date is the first day of the quarter or not.
Cú pháp
Thuộc tính
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
4 có cú pháp sau:Timestamp.is_quarter_start
Cú pháp cho thuộc tính .is_quarter_start trong gấu trúc
Giá trị tham số
Vì
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
4 là một thuộc tính, nó không có giá trị tham số.Giá trị trả về
Thuộc tính
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
4 trả về giá trị boolean cho biết ngày là ngày cuối cùng của quý (# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
7) hay không (# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
8).Thí dụ
# A code to illustrate the .is_quarter_end attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2022, 4, 30)
b = pd.Timestamp(2022, 4, 1)
# checking if date is the first day of the quarter
print(a.is_quarter_start)
print(b.is_quarter_start)
Thực hiện thuộc tính .is_quarter_start
Giải trình
- Dòng 3: Chúng tôi nhập thư viện
Timestamp.quarter
8. - Dòng 6 trận7: Chúng tôi tạo các đối tượng dấu thời gian,
Timestamp.is_quarter_start
0 vàTimestamp.is_quarter_start
1, đại diện cho tháng 6. - Dòng 10: Chúng tôi có được kết quả cho biết liệu có ngày nào là ngày đầu tiên của quý hay không, sử dụng
# A code to illustrate the quarter attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp(2022, 4, 29)
# obtaing the quarter of the given year
print("The given date can be found in quarter ", my_date.quarter)
4.
THẺ LIÊN QUAN
gấu trúc
khoa học dữ liệu
cộng đồng
Người đóng góp