gpt4 book ai didi

python - 从日期时间列中减去日期

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:17 25 4
gpt4 key购买 nike

我有一个数据框,其中一列(“ProcessingDATE”)是日期时间格式。我想创建另一列(“报告日期”),如果处理日期是星期一,则从中减去 3 天,最后将是星期五;否则减去 1 天。

我使用 python 的时间很短,所以不太了解如何编写它。我的想法是编写一个 for 循环 if the cell = Monday, then = datetime.datetime.today() – datetime.timedelta(days=3); else = datetime.datetime.today() – datetime.timedelta(days=1)

for j in range(len(DDA_compamy['ProcessingDATE'])):
if pd.to_datetime(datetime(DDA_company.ProcessingDATE[j])).weekday() == 2

最佳答案

希望这有帮助,

from datetime import timedelta

if DDA_compamy['ProcessingDATE'].weekday() == 4: #Condition to check if it is friday
DDA_compamy['Report Date']=DDA_compamy['ProcessingDATE'] - timedelta(days=3) # if friday subtracting 3 days
else:
DDA_compamy['Report Date']=DDA_compamy['ProcessingDATE'] - timedelta(days=1) #Else one day from the date is subtracted

上面也可以写成,

DDA_compamy['Report Date'] = (DDA_compamy['ProcessingDATE'] - timedelta(days=3)) if (DDA_compamy['ProcessingDATE'].weekday() == 4) else (DDA_compamy['Report Date']=DDA_compamy['ProcessingDATE'] - timedelta(days=1))

关于python - 从日期时间列中减去日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58209697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com