python汇总数据:日线->周线

背景

将浦发银行一年日线的收盘价汇总成周线数据。

代码

"""
数据按周汇总
"""
import numpy as np
import datetime

"""

step1 :读取数据

    2017浦发银行.CSV:
    时间,收盘价(浦发银行),成交量(浦发银行)
    2017-01-03,12.3804137039431,21377729.6808511
    2017-01-04,12.403199741435,39048562.9770213
    2017-01-05,12.3804137039431,34807692.2229787
    2017-01-06,12.2892695539754,22639651.1540426
    …………

"""

def datestr2date(s):
    s = s.decode('utf-8')
    #weekday函数返回 日期对应星期的天数,0代表星期一
    return datetime.datetime.strptime(s,"%Y-%m-%d").date()

filepath = r"C:\Users\tinysoft\Desktop\2017浦发银行.CSV"

#delimiter 参数指定了文件中的分隔符
#usecols 中的参数指定感兴趣的数据列
#unpack 参数设置为True使得不同列的数据分开存储
#skiprows 跳过一行
#dtype 设置转换过来的数据的格式
d,c,v = np.loadtxt(filepath,delimiter=',',skiprows=1,usecols=(0,1,2),unpack=True,
                dtype={"names":('date','close','vol'),
                       "formats":(np.object_,np.float32,np.float64)}
                   ,
               converters={0:datestr2date})

"""
step2:从日线数据统计出周线数据:
    开盘价,收盘价,最高价,最低价,实际开始日,实际截止日 
"""
weeksummary = []

start = 0
pwd = d[0].weekday()

len_d = len(d)
for i in range(1,len_d):

    wd = d[i].weekday()

    """
    判断两个日期之间夹着一个星期日(日期之间相差的天数如果大于7-星期几)
    """
    if (d[i]-d[i-1]).days > (6-d[i-1].weekday()):
        copen = c[start]    #开盘价
        cclose = c[i-1]     #收盘价
        cmax = np.max(c[start:i]) #最高价
        cmin = np.min(c[start:i]) #最低价
        dstart = d[start]   #实际开始日
        dendt = d[i-1]     #实际截止日
        weeksummary.append((copen,cclose,cmax,cmin,dstart,dendt))
        start = i
    pwd = wd

"""
step3:展示周线数据

"""
namesarr = ["实际开始日","实际截止日","开盘价","收盘价","最高价","最低价"]

#格式化时间样式
datefomat = lambda d : d.strftime("%Y-%m-%d") + "\t"
#格式化价格
cfomat = lambda close :  "%.2f"%close + "\t"

strs = "{0}".format(namesarr)

indexslist = [4,5,0,1,2,3]

for dicts in weeksummary:
    strs +="\n"
    for i in indexslist:
        strs += datefomat(dicts[i]) if i>3 else cfomat(dicts[i])

print(strs)

结果

>>> 
===== RESTART: E:/桌面space/临时数据/python/《python数据分析基础-numpy学习指南》/数据按周汇总.py =====
['实际开始日', '实际截止日', '开盘价', '收盘价', '最高价', '最低价']
2017-01-03  2017-01-06  12.38   12.29   12.40   12.29   
2017-01-09  2017-01-13  12.30   12.36   12.36   12.24   
2017-01-16  2017-01-20  12.58   12.61   12.61   12.46   
2017-01-23  2017-01-26  12.59   12.71   12.71   12.59   
2017-02-03  2017-02-03  12.63   12.63   12.63   12.63   
2017-02-06  2017-02-10  12.65   12.74   12.74   12.65   
2017-02-13  2017-02-17  12.80   12.64   12.80   12.64   
2017-02-20  2017-02-24  12.84   12.69   12.84   12.68   
2017-02-27  2017-03-03  12.60   12.42   12.60   12.42   
2017-03-06  2017-03-10  12.46   12.33   12.48   12.32   
2017-03-13  2017-03-17  12.41   12.30   12.41   12.30   
2017-03-20  2017-03-24  12.26   12.12   12.26   11.99   
2017-03-27  2017-03-31  12.18   12.16   12.18   12.01   
2017-04-05  2017-04-07  12.27   12.23   12.27   12.23   
2017-04-10  2017-04-14  12.13   11.78   12.13   11.78   
2017-04-17  2017-04-21  11.82   11.43   11.82   11.32   
2017-04-24  2017-04-28  11.39   11.55   11.55   11.39   
2017-05-02  2017-05-05  11.51   11.33   11.51   11.33   
2017-05-08  2017-05-12  11.29   11.55   11.55   11.10   
2017-05-15  2017-05-19  11.60   11.41   11.60   11.40   
2017-05-22  2017-05-26  11.42   12.84   12.93   11.42   
2017-05-31  2017-06-02  12.84   12.89   12.92   12.84   
2017-06-05  2017-06-09  12.85   12.93   12.93   12.80   
2017-06-12  2017-06-16  12.78   12.35   12.78   12.35   
2017-06-19  2017-06-23  12.40   12.64   12.64   12.29   
2017-06-26  2017-06-30  12.64   12.65   12.79   12.64   
2017-07-03  2017-07-07  12.56   12.60   12.66   12.55   
2017-07-10  2017-07-14  12.53   13.59   13.59   12.53   
2017-07-17  2017-07-21  13.76   13.45   13.76   13.45   
2017-07-24  2017-07-28  13.67   13.47   13.67   13.38   
2017-07-31  2017-08-04  13.36   12.87   13.44   12.87   
2017-08-07  2017-08-11  12.93   12.68   12.93   12.68   
2017-08-14  2017-08-18  12.56   12.51   12.59   12.49   
2017-08-21  2017-08-25  12.51   12.78   12.78   12.43   
2017-08-28  2017-09-01  12.89   12.77   12.96   12.71   
2017-09-04  2017-09-08  12.78   13.03   13.03   12.78   
2017-09-11  2017-09-15  12.99   12.83   13.04   12.83   
2017-09-18  2017-09-22  12.91   12.89   12.91   12.87   
2017-09-25  2017-09-29  12.94   12.87   12.94   12.84   
>>> 

你可能感兴趣的:(python_numpy,时间序列分析,python,numpy)