For a current project, I am planning to increase the value of a calendar date by 3 months with each iteration/loop. The value for start_date
should hence be [1st iteration]: 01/01/2017, [2nd interation] 01/04/2017
etc. For end_date
, it should accordingly yield [1st iteration]: 31/03/2017, [2nd interation] 30/06/2017
etc.
我最初认为这是解决问题的简单点,但是某种程度上脚本无法按计划工作(很多线程讨论如何一次性增加值,而不是在多个循环中执行此操作)。有谁知道如何调整事物?
相应的代码如下所示:
import datetime
from dateutil.relativedelta import *
for i in df.iterrows():
start_date = '01/01/2017'
start_date = start_date + relativedelta(months=+3)
end_date = '31/03/2017'
end_date = end_date + relativedelta(months=+3)
print(start_date)
print(end_date)
Your problem is that you are initialising the
start_date
andend_date
inside the loop. Just initialise them once outside the loop, then continue process like you're doing.