[英]Loop through Year while skipping certain months
I am using the following loop for a monthly time series and I would like to skip the months of May and November: 我在每月的时间序列中使用以下循环,但我希望跳过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"))
Can anyone suggest an approach? 谁能建议一种方法?
Using a simple if
condition to check if month not in check list. 使用简单的
if
条件检查月份是否不在检查清单中。
Ex: 例如:
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.