0
点赞
收藏
分享

微信扫一扫

pandas从日数据计算周、月、季度、年数据

闲云困兽 2022-01-08 阅读 54

1. 计算逻辑

日数据包含的字段有【日期,开盘价,收盘价,最低价,最高价,成交量,成交金额】,从日数据计算周数据、月数据、季度数据、年数据逻辑:

日数据日期开盘价收盘价最低价最高价成交量成交金额
周数据每周五日期每周第一日开盘价每周最后一日收盘价本周最低价本周最高价本周每日成交量加和本周每日成交金额加和
月数据每个月最后一日每月第一日开盘价每月最后一日收盘价本月最低价本月最高价本月每日成交量加和本月每日成交金额加和
季度数据每季度最后一日每季度第一日开盘价每季度最后一日收盘价本季度最低价本季度最高价本季度每日成交量加和本季度每日成交金额加和
年数据每年最后一日每年第一日开盘价每年最后一日收盘价本年最低价本年最高价本年每日成交量加和本年每日成交金额加和

附加说明:表格中提交的“第一日”,“最后一日”,“每日”都指的是交易日

2. 计算过程

以000001日数据为例

dateopencloselowhighvolumemoney
2005/1/41.471.461.451.47786751911465603
2005/1/51.461.451.421.471439676120718559
2005/1/61.451.461.441.471191371717333840
2005/1/71.471.461.451.48842745212302853
2005/1/101.461.471.431.471176020317111498
2005/1/111.471.471.471.4700
2005/1/121.481.461.451.48732168610672635
2005/1/131.461.471.451.48715236810504590
2005/1/141.471.451.451.48717022310476658
2005/1/171.451.41.391.451417006520039971
2005/1/181.41.391.381.411158027816109620
2005/1/191.391.381.381.4753045610416563
2005/1/201.381.351.341.381764412723815009
2005/1/211.341.431.311.454484986762408193

column=['date','open','close','low','high','volume','money']

df = pd.read_csv('000001.csv',encoding='utf-8')
df['date'] = pd.to_datetime(df['date'])
week_group = df.resample('W-FRI',on='date')
month_group = df.resample('M',on='date')
quarter_group = df.resample('Q',on='date')
year_group = df.resample('A',on='date')

week_df = week_group.last()
week_df['endDate'] = week_df.index.get_level_values('date')
week_df['open'] = week_group.first()['open']
week_df['low'] = week_group.min()['low']
week_df['high'] = week_group.max()['high']
week_df['volume'] = week_group.sum()['volume']
week_df['money'] = week_group.sum()['money']
week_df.rename(columns={'date':'ori_date','endDate':'date'},inplace=True)

month_df = month_group.last()
month_df['endDate'] = month_df.index.get_level_values('date')
month_df['open'] = month_group.first()['open']
month_df['low'] = month_group.min()['low']
month_df['high'] = month_group.max()['high']
month_df['volume'] = month_group.sum()['volume']
month_df['money'] = month_group.sum()['money']
month_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)

quarter_df = quarter_group.last()
quarter_df['endDate'] = quarter_df.index.get_level_values('date')
quarter_df['open'] = quarter_group.first()['open']
quarter_df['low'] = quarter_group.min()['low']
quarter_df['high'] = quarter_group.max()['high']
quarter_df['volume'] = quarter_group.sum()['volume']
quarter_df['money'] = quarter_group.sum()['money']
quarter_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)

year_df = year_group.last()
year_df['endDate'] = year_df.index.get_level_values('date')
year_df['open'] = year_group.first()['open']
year_df['low'] = year_group.min()['low']
year_df['high'] = year_group.max()['high']
year_df['volume'] = year_group.sum()['volume']
year_df['money'] = year_group.sum()['money']
year_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)

附加说明:周数据重采样W-FRI表示取周五的数据,否则默认W是取得周日的数据

举报

相关推荐

0 条评论