1,读入数据
names = ['date',
'time',
'opening_price',
'ceiling_price',
'floor_price',
'closing_price',
'volume',
'amount']
raw = pd.read_csv('SH600690.csv', names = names, header = None, index_col='date', parse_dates=True)
打印前几行
time opening_price ceiling_price floor_price closing_price \
date
2008-01-02 09:35 22.50 22.63 22.50 22.51
2008-01-02 09:40 22.51 22.51 22.29 22.37
2008-01-02 09:45 22.39 22.62 22.38 22.62
2008-01-02 09:50 22.60 23.00 22.60 22.95
2008-01-02 09:55 22.98 23.20 22.89 23.20
volume amount
date
2008-01-02 2042.50 4604723.0
2008-01-02 1545.17 3460503.0
2008-01-02 1744.76 3921443.0
2008-01-02 5339.00 12225939.0
2008-01-02 12577.73 28947824.0
2,根据涨跌幅判断数据是否有效
def _valid_price(prices):
return (((prices.max() - prices.min()) / prices.min()) < 0.223).all()
3,按日期分组
days = raw.groupby(level = 0).agg(
{'opening_price':lambda prices: _valid_price(prices) and prices[0] or 0,
'ceiling_price':lambda prices: _valid_price(prices) and np.max(prices) or 0,
'floor_price':lambda prices: _valid_price(prices) and np.min(prices) or 0,
'closing_price':lambda prices: _valid_price(prices) and prices[-1] or 0,
'volume':'sum',
'amount':'sum'})