df = spark.createDataFrame(
[(2019,), (2020,), (2021,), (2022,), (2023,)], ["year_col"]
)
df = (
df.withColumn(
"last_day", last_day(concat(col("year_col"), lit("-03-01")))
)
.withColumn(
"last_day_number", date_format("last_day", "u") # 7 = Sunday
)
.withColumn(
"last_sunday_of_march",
when(
col("last_day_number") == lit(7), col("last_day") # if last day of march is sunday, then it is the last sunday of march
).otherwise(
expr("date_sub(last_day, last_day_number)") # else, subtract the number of days from the last day of march to get the last sunday of march
),
)
).select("year_col", "last_sunday_of_march")
2条答案
按热度按时间piok6c0g1#
我觉得夏令时在这里不重要。你可以通过查找Apr/1的dayofweek来查找3月的最后一个星期日,然后从Apr/1中减去它。
字符串
测试结果
型
xxe27gdn2#
以下是步骤:
last_day
列)last_day_number
列)代码:
字符串
测试结果:
型