我正在寻找一个日期范围,其中start_date和end_date每月增加一次,每次迭代都从月初开始。
第一个迭代示例:
start_date = '2020-01-01'
end_date = '2020-02-01'
循环的第二次迭代应类似于:
start_date = '2020-02-01'
end_date = '2020-03-01'
我尝试过的
for x in range(20):
start_date = pd.Timestamp('2020-01-01') + pd.offsets.MonthBegin(n=1)
end_date = start_date + pd.offsets.MonthBegin(n=1)
我遇到的问题是,在下一次迭代中,我不会增加到下个月。它停留在当前月份。有没有办法增加到下个月?
start_date = pd.to_datetime('2020-02-01')
end_date = pd.to_datetime('2020-03-01')
for x in range(20):
print(start_date.strftime('%Y-%m-%d'))
print(end_date.strftime('%Y-%m-%d'))
# do the work here...
# then increment the dates for the next iteration
start_date = start_date + pd.offsets.MonthBegin(n=1)
end_date = start_date + pd.offsets.MonthBegin(n=1)
似乎能胜任
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句