我想创建一个循环,在提供的时间段、第一天和最后一天返回每个月(考虑到月份在第 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
我是一名优秀的程序员,十分优秀!