遍历日历月的开始/结束

时间:2019-06-17 15:18:32

标签: python loops date

我想创建一个循环,在提供时间段的第一天和最后一天返回每个月(考虑到月份在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'

有人可以建议一种方法吗?

4 个答案:

答案 0 :(得分:2)

好的,这是我对您的问题的实现:

import calendar

year = 2014

for i in range(1,7):
    start_date = f'{year}-0{i}-01'
    end_date = calendar.monthrange(year, {i})[1]

答案 1 :(得分:2)

使用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

答案 2 :(得分:1)

这项工作可以吗?

for i in range(1, 7):  # 1 through 6 inclusive
        period_start = f'2014-0{i}-01'
        period_end = (datetime.date(2014, i+1, 1) - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

这不会使用pandas日期范围,而只是在i的{​​{1}}字段中插入month。获取period_start的月份的最后一天比较麻烦,但是一种解决方法是获取下一个月的第一天,然后从中减去一天。在这里,我使用datetime完成此操作。

答案 3 :(得分:1)

我们可以使用python中的 datetime calendar 模块组合使用

def get_start_end_dates(from_date, to_date):
  # Convert string to datetime objects
  from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d')
  to_date = datetime.datetime.strptime(to_date, '%Y-%m-%d')

  # The beginning day is always 1
  beg_date = datetime.datetime(from_date.year, from_date.month, 1)

  # Iterate till the beginning date is less the to date
  while beg_date <= to_date:
    # Get the number of days in that month in that year
    n_days_in_that_month = calendar.monthrange(beg_date.year, beg_date.month)[1]

    # Get end date using n_days_in_that_month
    end_date = datetime.datetime(beg_date.year, beg_date.month, n_days_in_that_month)

    # Yield the beg_date and end_date
    yield (beg_date.date(), end_date.date())

    # Next month's first day will be end_date + 1 day
    beg_date = end_date + datetime.timedelta(days=1)

for period_start, period_end in get_start_end_dates('2018-02-01', '2019-01-01'):
    print ('period_start: {}'.format(period_start), 'period_end: {}'.format(period_end))

以上代码的结果如下。

period_start: 2018-02-01 period_end: 2018-02-28
period_start: 2018-03-01 period_end: 2018-03-31
period_start: 2018-04-01 period_end: 2018-04-30
period_start: 2018-05-01 period_end: 2018-05-31
period_start: 2018-06-01 period_end: 2018-06-30
period_start: 2018-07-01 period_end: 2018-07-31
period_start: 2018-08-01 period_end: 2018-08-31
period_start: 2018-09-01 period_end: 2018-09-30
period_start: 2018-10-01 period_end: 2018-10-31
period_start: 2018-11-01 period_end: 2018-11-30
period_start: 2018-12-01 period_end: 2018-12-31
period_start: 2019-01-01 period_end: 2019-01-31

希望有帮助!