import pandas as pd from datetime import datetime def datelist(beginDate, endDate): # beginDate, endDate是形如‘20160601’的字符串或datetime格式 date_l=[datetime.strftime(x,'%Y-%m') for x in list(pd.date_range(start=beginDate, end=endDate))] date_set=list(set(date_l)) date_set.sort(key=date_l.index) return date_set 输出: ['2009-06', '2009-07', '2009-08', '2009-09', '2009-10', '2009-11']