gpt4 book ai didi

python - 遍历日历月的开始/结束

转载 作者:太空宇宙 更新时间:2023-11-04 07:49:50 28 4
gpt4 key购买 nike

我想创建一个循环,在提供的时间段、第一天和最后一天返回每个月(考虑到月份在第 28-31 天结束):(“function_to_increase_month”尚未定义)

for beg in pd.date_range('2014-01-01', '2014-06-30', freq='1M'):
period_start = beg
period_end = function_to_increase_month(beg)

第一次迭代的预期输出: period_start = '2014-01-01' period_end = '2014-01-31'

第二次迭代: period_start = '2014-02-01' period_end = '2014-02-28'

第三次迭代: period_start = '2014-03-01' period_end = '2014-03-31'

谁能推荐一种方法?

最佳答案

使用 pandas.tseries.offsets.MonthEnd

例如:

from pandas.tseries.offsets import MonthEnd

for beg in pd.date_range('2014-01-01', '2014-06-30', freq='MS'):
print(beg.strftime("%Y-%m-%d"), (beg + MonthEnd(1)).strftime("%Y-%m-%d"))

输出:

2014-01-01 2014-01-31
2014-02-01 2014-02-28
2014-03-01 2014-03-31
2014-04-01 2014-04-30
2014-05-01 2014-05-31
2014-06-01 2014-06-30

关于python - 遍历日历月的开始/结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56634265/

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