# -*- coding: utf-8 -*-
"""
Created on Fri May 24 15:17:54 2019
@author: User
"""
import pandas as pd
import numpy as np
jddf=pd.read_csv('data\stock.csv', header=None,
names=['id','time','code','open_p',
'close_p','low_p','vol','high_p','col'])
print(jddf.head())
print(jddf['time'].head())
print("\n 索引转为日期格式:")
jd_ts=jddf.set_index(pd.to_datetime(jddf['time'],format="%Y%m%d"))
print(jd_ts.index)
print("\n jd_ts['2018-04-01':'2018-04-06'][['open_p','close_p']]:")
sample=jd_ts['2019-03-13':'2019-03-19'][['code','open_p','close_p']]
print(sample)
print("\n 把sample时序数据向后移动2期:")
print(sample.shift(2) )
print("\n 把sample时序索引移动,数据不变:")
print(sample.shift(-2,freq='1D'))
print("\n 自定义一个时间序列:")
date=pd.date_range('2019-03-13','2019-03-19',freq='D')
s1=pd.DataFrame({'open_p':np.random.randn(7),'close_p':np.random.randn(7)},
index=date)
print(s1)
print("\n 为前面的sample中的开盘收盘价格加上随机干扰:")
print(sample + s1)
运行:
id time code open_p close_p low_p vol high_p col
0 1 20130902 600028 4.41 4.43 4.37 17275.39 4.41 392662
1 2 20130903 600028 4.41 4.46 4.40 19241.84 4.45 434177
2 3 20130904 600028 4.44 4.49 4.42 20106.30 4.47 451470
3 4 20130905 600028 4.47 4.48 4.42 15582.48 4.47 349997
4 5 20130906 600028 4.46 4.52 4.45 19101.41 4.50 425777
0 20130902
1 20130903
2 20130904
3 20130905
4 20130906
Name: time, dtype: int64
索引转为日期格式:
DatetimeIndex(['2013-09-02', '2013-09-03', '2013-09-04', '2013-09-05',
'2013-09-06', '2013-09-09', '2013-09-10', '2013-09-11',
'2013-09-12', '2013-09-13',
...
'2019-03-18', '2019-03-19', '2019-03-20', '2019-03-21',
'2019-03-22', '2019-03-25', '2019-03-26', '2019-03-27',
'2019-03-28', '2019-03-29'],
dtype='datetime64[ns]', name='time', length=3982, freq=None)
jd_ts['2018-04-01':'2018-04-06'][['open_p','close_p']]:
code open_p close_p
time
2019-03-13 600028 5.98 5.98
2019-03-14 600028 5.93 6.00
2019-03-15 600028 5.94 5.98
2019-03-18 600028 5.95 6.07
2019-03-19 600028 6.08 6.10
2019-03-13 601857 7.75 7.76
2019-03-14 601857 7.72 7.79
2019-03-15 601857 7.68 7.72
2019-03-18 601857 7.69 7.82
2019-03-19 601857 7.83 7.86
2019-03-13 600019 7.46 7.48
2019-03-14 600019 7.37 7.46
2019-03-15 600019 7.30 7.38
2019-03-18 600019 7.34 7.58
2019-03-19 600019 7.53 7.58
把sample时序数据向后移动2期:
code open_p close_p
time
2019-03-13 NaN NaN NaN
2019-03-14 NaN NaN NaN
2019-03-15 600028.0 5.98 5.98
2019-03-18 600028.0 5.93 6.00
2019-03-19 600028.0 5.94 5.98
2019-03-13 600028.0 5.95 6.07
2019-03-14 600028.0 6.08 6.10
2019-03-15 601857.0 7.75 7.76
2019-03-18 601857.0 7.72 7.79
2019-03-19 601857.0 7.68 7.72
2019-03-13 601857.0 7.69 7.82
2019-03-14 601857.0 7.83 7.86
2019-03-15 600019.0 7.46 7.48
2019-03-18 600019.0 7.37 7.46
2019-03-19 600019.0 7.30 7.38
把sample时序索引移动,数据不变:
code open_p close_p
time
2019-03-11 600028 5.98 5.98
2019-03-12 600028 5.93 6.00
2019-03-13 600028 5.94 5.98
2019-03-16 600028 5.95 6.07
2019-03-17 600028 6.08 6.10
2019-03-11 601857 7.75 7.76
2019-03-12 601857 7.72 7.79
2019-03-13 601857 7.68 7.72
2019-03-16 601857 7.69 7.82
2019-03-17 601857 7.83 7.86
2019-03-11 600019 7.46 7.48
2019-03-12 600019 7.37 7.46
2019-03-13 600019 7.30 7.38
2019-03-16 600019 7.34 7.58
2019-03-17 600019 7.53 7.58
自定义一个时间序列:
open_p close_p
2019-03-13 -0.507051 0.601905
2019-03-14 -0.370236 -0.117281
2019-03-15 0.214149 -0.479078
2019-03-16 -1.180924 -0.982182
2019-03-17 -0.240209 -0.402124
2019-03-18 0.117047 0.685900
2019-03-19 0.694675 0.001176
为前面的sample中的开盘收盘价格加上随机干扰:
close_p code open_p
2019-03-13 6.581905 NaN 5.472949
2019-03-13 8.361905 NaN 7.242949
2019-03-13 8.081905 NaN 6.952949
2019-03-14 5.882719 NaN 5.559764
2019-03-14 7.672719 NaN 7.349764
2019-03-14 7.342719 NaN 6.999764
2019-03-15 5.500922 NaN 6.154149
2019-03-15 7.240922 NaN 7.894149
2019-03-15 6.900922 NaN 7.514149
2019-03-16 NaN NaN NaN
2019-03-17 NaN NaN NaN
2019-03-18 6.755900 NaN 6.067047
2019-03-18 8.505900 NaN 7.807047
2019-03-18 8.265900 NaN 7.457047
2019-03-19 6.101176 NaN 6.774675
2019-03-19 7.861176 NaN 8.524675
2019-03-19 7.581176 NaN 8.224675