目標是分別根據以下用戶定義的 2021 和 2022 日歷將 Pandas 時間戳物件轉換為一年中的第幾周。


我正在使用 pandas datetime 的 week 屬性,該屬性適用于 2021 年的日期,但它們將在明年崩潰。這是我寫的初始函式。
def week(date: pandas.Timestamp) -> int:
"""Convert the date to a week according to client calendar."""
orig_week: int = date.week % 53
return orig_week 1 if date.dayofweek < 5 else orig_week 2
我添加了模 53,因為如果沒有它,有時其余的邏輯會給我像 54 之類的數字。但是我不確定 pandas week 屬性的內部邏輯,因此無法真正掌握如何將那一周轉換為上述用戶定義的日歷,盡管這聽起來像是一個簡單的轉變。問題圍繞邊緣情況(年底或年初)。所以任何幫助將不勝感激。
uj5u.com熱心網友回復:
一種解決方案是為輸入年份創建自定義日歷。一年中的日期將轉換為一個 Period,表示從星期六開始到星期五結束的周。
import pandas as pd
def week(in_date: pd.Timestamp) -> int:
"""Convert the date to a week according to client calendar."""
# Setup a custom calendar table for the year of input date
in_date_year = str(in_date.year)
_df = pd.DataFrame({'Date':pd.date_range(in_date_year '-1-1', in_date_year '-12-31')})
_df['Period'] = _df['Date'].dt.to_period('W-FRI') # define week period that starts on SAT and ends on FRI
_df['Week_Num'] = _df['Period'].dt.week
# Adjust week number for year start
_df['Week_Num'] = np.where(_df['Week_Num'].iloc[0] >= 52, _df['Week_Num'] % 53 1, _df['Week_Num'])
# Adjust week number for year end
_df.iloc[-7:, _df.columns.get_loc('Week_Num')] = np.where(_df['Week_Num'].iloc[-7:] < 52, 53, _df['Week_Num'].iloc[-7:])
# Get week number and return
return _df.loc[_df['Date'] == in_date, 'Week_Num'].iat[0]
為 Period 回傳的周數將根據從 SAT 開始到 FRI 結束的周數進行設定。但是,對于年頭和年末,周數仍可能顯示與上一年/下一年對應的周數。因此,我們相應地檢查和調整了今年的開始/結束差異。
結果:
week(pd.Timestamp('2022-01-01'))
#output
1
week(pd.Timestamp('2022-12-31'))
#output
53
底層表構建如下:
對于 2022 年:
# print first 10 rows of the year
print(_df.head(10))
Date Period Week_Num
0 2022-01-01 2022-01-01/2022-01-07 1
1 2022-01-02 2022-01-01/2022-01-07 1
2 2022-01-03 2022-01-01/2022-01-07 1
3 2022-01-04 2022-01-01/2022-01-07 1
4 2022-01-05 2022-01-01/2022-01-07 1
5 2022-01-06 2022-01-01/2022-01-07 1
6 2022-01-07 2022-01-01/2022-01-07 1
7 2022-01-08 2022-01-08/2022-01-14 2
8 2022-01-09 2022-01-08/2022-01-14 2
9 2022-01-10 2022-01-08/2022-01-14 2
# print last 10 rows of the year
print(_df.tail(10))
Date Period Week_Num
355 2022-12-22 2022-12-17/2022-12-23 51
356 2022-12-23 2022-12-17/2022-12-23 51
357 2022-12-24 2022-12-24/2022-12-30 52
358 2022-12-25 2022-12-24/2022-12-30 52
359 2022-12-26 2022-12-24/2022-12-30 52
360 2022-12-27 2022-12-24/2022-12-30 52
361 2022-12-28 2022-12-24/2022-12-30 52
362 2022-12-29 2022-12-24/2022-12-30 52
363 2022-12-30 2022-12-24/2022-12-30 52
364 2022-12-31 2022-12-31/2023-01-06 53
對于 2025 年:
# print first 10 rows of the year
print(_df.head(10))
Date Period Week_Num
0 2025-01-01 2024-12-28/2025-01-03 1
1 2025-01-02 2024-12-28/2025-01-03 1
2 2025-01-03 2024-12-28/2025-01-03 1
3 2025-01-04 2025-01-04/2025-01-10 2
4 2025-01-05 2025-01-04/2025-01-10 2
5 2025-01-06 2025-01-04/2025-01-10 2
6 2025-01-07 2025-01-04/2025-01-10 2
7 2025-01-08 2025-01-04/2025-01-10 2
8 2025-01-09 2025-01-04/2025-01-10 2
9 2025-01-10 2025-01-04/2025-01-10 2
# print last 10 rows of the year
print(_df.tail(10))
Date Period Week_Num
355 2025-12-22 2025-12-20/2025-12-26 52
356 2025-12-23 2025-12-20/2025-12-26 52
357 2025-12-24 2025-12-20/2025-12-26 52
358 2025-12-25 2025-12-20/2025-12-26 52
359 2025-12-26 2025-12-20/2025-12-26 52
360 2025-12-27 2025-12-27/2026-01-02 53
361 2025-12-28 2025-12-27/2026-01-02 53
362 2025-12-29 2025-12-27/2026-01-02 53
363 2025-12-30 2025-12-27/2026-01-02 53
364 2025-12-31 2025-12-27/2026-01-02 53
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337071.html
