我将以下循环用于每月的时间序列,但我想跳过5月和11月:

from pandas.tseries.offsets import MonthEnd

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


谁能建议一种方法?

最佳答案

使用简单的if条件检查月份是否不在检查列表中。

例如:

import pandas as pd
from pandas.tseries.offsets import MonthEnd

for beg in pd.date_range('2014-01-01', '2017-12-31', freq='MS'):
    if not beg.month in [5, 11]:
        print(beg.strftime("%Y-%m-%d"), (beg + MonthEnd(1)).strftime("%Y-%m-%d"))

09-07 04:49