df 是保存日数据的 dataframe,格式如下:
想取每个月、季度、半年最后一天,其思路是,先取每个日期的年-月信息,然后再将不符合季度/半年的日期去掉,剩下的取将年-月信息重复的去掉,只保留同类中的最后一项:
...
df['year-month'] = [str(i)[0:7] for i in df.index]
if freq == 'month':
new_index = df.drop_duplicates('year-month', keep='last').index
elif freq == 'quarter':
df['month'] = [str(i)[5:7] for i in df.index]
df = df[(df['month'] in ['01', '1']) | (df['month'] in ['04', '4']) | (df['month'] in ['07', '7’]) | (df['month'] in ['10']) ]
new_index = df.drop_duplicates('year-month', keep='last').index
elif freq in ['half_year', 'halfyear', 'year/2']:
df['month'] = [str(i)[5:7] for i in df.index]
df = df[(df['month']=='01') | (df['month']=='06')]
new_index = df.drop_duplicates('year-month', keep='last').index
else:
new_index = df.index
...