我有一個資料框,我正在嘗試添加一個帶有target_date' 期間開始日期的列。但是由于閏年的開始日期,我變得空了。在這里感謝您的幫助。
----- ---------- ---------- ------------ ------- -----------------------
| id|start_date| end_date|target_date_|period_|target_date_fiscal_year|
----- ---------- ---------- ------------ ------- -----------------------
|34667|2017-12-30|2022-12-30| 2021-11-30| 5| 2020|
|47353|2020-02-10|2023-02-10| 2021-11-30| 3| 2021|
|94773|2017-04-15|2022-04-15| 2021-11-30| 5| 2021|
|67324|2017-11-25|2022-11-25| 2021-11-30| 5| 2021|
|45688|2020-02-29|2025-02-28| 2021-11-30| 5| 2021|
----- ---------- ---------- ------------ ------- -----------------------
預期輸出:
----- ---------- ---------- ------------ ------- ----------------------- --------------------
| id|start_date| end_date|target_date_|period_|target_date_fiscal_year|period_starting_date|
----- ---------- ---------- ------------ ------- ----------------------- --------------------
|34667|2017-12-30|2022-12-30| 2021-11-30| 5| 2020| 2020-12-30|
|47353|2020-02-10|2023-02-10| 2021-11-30| 3| 2021| 2021-02-10|
|94773|2017-04-15|2022-04-15| 2021-11-30| 5| 2021| 2021-04-15|
|67324|2017-11-25|2022-11-25| 2021-11-30| 5| 2021| 2021-11-25|
|45688|2020-02-29|2025-02-28| 2021-11-30| 5| 2021| 2021-02-28|
----- ---------- ---------- ------------ ------- ----------------------- --------------------
我嘗試了下面的代碼并沒有得到正確的輸出。
df.withColumn("period_starting_date", F.concat(F.col('target_date_fiscal_year'),
F.substring(F.col("start_date"), -6, 6)).cast('date')).show()
----- ---------- ---------- ------------ ------- ----------------------- --------------------
| id|start_date| end_date|target_date_|period_|target_date_fiscal_year|period_starting_date|
----- ---------- ---------- ------------ ------- ----------------------- --------------------
|34667|2017-12-30|2022-12-30| 2021-11-30| 5| 2020| 2020-12-30|
|47353|2020-02-10|2023-02-10| 2021-11-30| 3| 2021| 2021-02-10|
|94773|2017-04-15|2022-04-15| 2021-11-30| 5| 2021| 2021-04-15|
|67324|2017-11-25|2022-11-25| 2021-11-30| 5| 2021| 2021-11-25|
|45688|2020-02-29|2025-02-28| 2021-11-30| 5| 2021| null|
----- ---------- ---------- ------------ ------- ----------------------- --------------------
uj5u.com熱心網友回復:
target_date_fiscal_year您可以計算和 的年份之間的差異start_date,然后將結果相加start_date得到period_starting_date:
from pyspark.sql import functions as F
df1 = df.withColumn(
"period_starting_date",
F.to_date("start_date") F.format_string(
"interval %s year", F.col("target_date_fiscal_year") - F.year("start_date")
).cast("interval")
)
df1.show()
# ----- ---------- ---------- ------------ ------- ----------------------- --------------------
#| id|start_date| end_date|target_date_|period_|target_date_fiscal_year|period_starting_date|
# ----- ---------- ---------- ------------ ------- ----------------------- --------------------
#|34667|2017-12-30|2022-12-30| 2021-11-30| 5| 2020| 2020-12-30|
#|47353|2020-02-10|2023-02-10| 2021-11-30| 3| 2021| 2021-02-10|
#|94773|2017-04-15|2022-04-15| 2021-11-30| 5| 2021| 2021-04-15|
#|67324|2017-11-25|2022-11-25| 2021-11-30| 5| 2021| 2021-11-25|
#|45688|2020-02-29|2025-02-28| 2021-11-30| 5| 2021| 2021-02-28|
# ----- ---------- ---------- ------------ ------- ----------------------- --------------------
uj5u.com熱心網友回復:
在 Python 中有一個漂亮的包叫做dateutil,它可以幫助你解決你的問題。
注意:您沒有添加代碼,因此無法檢查這是否 100% 正確。
from dateutil.relativedelta import relativedelta
def delta_creator(df):
delta = df['target_date_fiscal_year'] - df['start_date'].dt.year
df['period_starting_date'] = df['start_date'] relativedelta(years=delta)
return df
df = df.apply(delta_creator, axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/419845.html
標籤:
上一篇:我想累計計算先前重復值的數量
