我构建了一个例程,它使用不同的频率和句点创建了一个日期列表,我唯一无法工作的是使用句点,同时使用“M”表示月末条目。
def run_date_creator(start_date, end_date, steps, interval):
#Interval: Days(d or D), Weeks (w or W), Month (m or M), Quarters (q or Q) and Years(y or Y)
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)
if interval == 'd' or interval == 'D':
dates = pd.date_range(start_date, end_date, freq=pd.tseries.offsets.DateOffset(days=1*steps))
elif interval == 'w':
dates = pd.date_range(start_date, end_date, freq=pd.tseries.offsets.DateOffset(days=7*steps))
elif interval == 'bm' or interval == 'BM':
dates = pd.date_range(start_date, end_date, freq=pd.tseries.offsets.DateOffset(months=1*steps))
elif interval == 'em' or interval == 'EM':
dates = pd.date_range(start_date, end_date, freq='M')
elif interval == 'q' or interval == 'Q':
dates = pd.date_range(start_date, end_date, freq=pd.tseries.offsets.DateOffset(months=3*steps))
elif interval == 'y' or interval == 'Y':
dates = pd.date_range(start_date, end_date, freq=pd.tseries.offsets.DateOffset(months=12*steps))
return list(dates.strftime("%Y-%m-%d"))
start_date = '2017-1-1'
end_date = '2017-12-31'
dates = run_date_creator(start_date, end_date , 2, 'em')
提前感谢。
答案 0 :(得分:1)
您可以在freq
中指定步骤。
dates = pd.date_range(start_date, end_date, freq='%dM' % steps)