PythonDataframe时间序列处理,日线转季周月年线
(2018-09-18 10:59:22)
标签:
python日线月线季线dataframe |
分类: Python |
用Tushare获得股票历史交易数据并存放在Dataframe中,名为df,结构如下,部分列为后处理,不重要
| ts_code | trade_date | open | high | low | close | pre_close | change | pct_change | vol | amount | report_date | report_year | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 000001.SZ | 20180820 | 8.90 | 9.02 | 8.87 | 9.00 | 8.81 | 0.19 | 2.1566 | 681322.06 | 609934.725 | 20180630 | 20171231 |
现在需要将其转换为季线,从而和财报结合观测数据,具体代码如下
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#转换YYYYMMDD格式的字符串为YYYY-MM-DD
def df['trade_date']=df['trade_date'].apply(str_to_date) #转换为日期格式,否则无法转换季线 df['trade_date'] # #必须将日期格式作为索引,才能做resample df.set_index('trade_date',inplace=True) #原来的做法是 #日期用最后那天的日期 #开盘取第一天,收盘去最后一天,最高和最低分别取最大最小值,成交量和成交金额取求和值 #这里也不用how的方法 df_quote['change'] df_quote['open'] df_quote['close'] df_quote['high'] df_quote['low'] df_quote['vol'] df_quote['amount'] #过滤整季度无交易的数据 df_quote #从新索引 df_quote.reset_index(inplace=True) df.reset_index(inplace=True) df_quote |

加载中…