'''
【课程2.8】 时间模块:datetime
datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta()
日期解析方法:parser.parse
'''
'\n【课程2.8】 时间模块:datetime\n\ndatetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta()\n\n日期解析方法:parser.parse\n\n'
import datetime
today=datetime.date.today()
print(today,type(today))
t=datetime.date(2016,1,1)
print(t,type(t))
2019-07-01
2016-01-01
now=datetime.datetime.now()
t1=datetime.datetime(2019,6,26)
t2=datetime.datetime(2019,6,26,11,55,55)
print(now,type(now))
print(str(now),type(str(now)))
print(t1,type(t1))
print(t2,type(t2))
tdelta=t2-t1
print(tdelta,type(tdelta))
2019-07-01 12:55:12.485330
2019-07-01 12:55:12.485330
2019-06-26 00:00:00
2019-06-26 11:55:55
11:55:55
today=datetime.datetime.today()
yestoday=today-datetime.timedelta(1)
print(today)
print(yestoday)
2019-07-01 12:55:12.492313
2019-06-30 12:55:12.492313
from dateutil.parser import parse
date='12-21-2017'
t=parse(date)
print(t,type(t))
print(parse('2000-1-1'),'\n',
parse('5/1/2014'),'\n',
parse('5/1/2014', dayfirst = True),'\n',
parse('22/1/2014'),'\n',
parse('Jan 31, 1997 10:45 PM'))
2017-12-21 00:00:00
2000-01-01 00:00:00
2014-05-01 00:00:00
2014-01-05 00:00:00
2014-01-22 00:00:00
1997-01-31 22:45:00
import datetime
d1=datetime.datetime.now()
print('请输出当前时间:',d1)
d2=datetime.datetime(2017,5,1)
print('请输出时间:',d2)
d3=datetime.datetime(2000,12,1)
print('请输出时间:',d3)
请输出当前时间: 2019-07-01 12:55:12.506274
请输出时间: 2017-05-01 00:00:00
请输出时间: 2000-12-01 00:00:00
d=datetime.datetime(2000,5,1)
print(d+datetime.timedelta(1000))
2003-01-26 00:00:00
'''
【课程2.9】 Pandas时刻数据:Timestamp
时刻数据代表时间点,是pandas的数据类型,是将值与时间点相关联的最基本类型的时间序列数据
pandas.Timestamp()
'''
'\n【课程2.9】 Pandas时刻数据:Timestamp\n\n时刻数据代表时间点,是pandas的数据类型,是将值与时间点相关联的最基本类型的时间序列数据\n\npandas.Timestamp()\n\n'
import numpy as np
import pandas as pd
date1=datetime.datetime(2016,12,1,12,12,12)
date2='2017 12 21'
t1=pd.Timestamp(date1)
t2=pd.Timestamp(date2)
print(t1,type(t1))
print(t2,type(t2))
2016-12-01 12:12:12
2017-12-21 00:00:00
from datetime import datetime
date1 = datetime(2016,12,1,12,45,30)
date2 = '2017-12-21'
t1 = pd.to_datetime(date1)
t2 = pd.to_datetime(date2)
print(t1,type(t1))
print(t2,type(t2))
lst_date=['2017-12-21', '2017-12-22', '2017-12-23']
t3=pd.to_datetime(lst_date)
print(t3,type(t3))
2016-12-01 12:45:30
2017-12-21 00:00:00
DatetimeIndex(['2017-12-21', '2017-12-22', '2017-12-23'], dtype='datetime64[ns]', freq=None)
date1 = [datetime(2015,6,1),datetime(2015,7,1),datetime(2015,8,1),datetime(2015,9,1),datetime(2015,10,1)]
date2 = ['2017-2-1','2017-2-2','2017-2-3','2017-2-4','2017-2-5','2017-2-6']
print(date1)
print(date2)
t1 = pd.to_datetime(date2)
t2 = pd.to_datetime(date2)
print(t1)
print(t2)
date3 = ['2017-2-1','2017-2-2','2017-2-3','hello world!','2017-2-5','2017-2-6']
t3 = pd.to_datetime(date3, errors = 'ignore')
print(t3,type(t3))
t4 = pd.to_datetime(date3, errors = 'coerce')
print(t4,type(t4))
[datetime.datetime(2015, 6, 1, 0, 0), datetime.datetime(2015, 7, 1, 0, 0), datetime.datetime(2015, 8, 1, 0, 0), datetime.datetime(2015, 9, 1, 0, 0), datetime.datetime(2015, 10, 1, 0, 0)]
['2017-2-1', '2017-2-2', '2017-2-3', '2017-2-4', '2017-2-5', '2017-2-6']
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04',
'2017-02-05', '2017-02-06'],
dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04',
'2017-02-05', '2017-02-06'],
dtype='datetime64[ns]', freq=None)
Index(['2017-2-1', '2017-2-2', '2017-2-3', 'hello world!', '2017-2-5',
'2017-2-6'],
dtype='object')
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', 'NaT', '2017-02-05',
'2017-02-06'],
dtype='datetime64[ns]', freq=None)
import numpy as np
import pandas as pd
lst=[]
for i in range(1,32):
lst.append('2017-12-%i'%i)
print(lst)
d=pd.to_datetime(lst)
print(d)
print('月中日期为:\n',d[16])
['2017-12-1', '2017-12-2', '2017-12-3', '2017-12-4', '2017-12-5', '2017-12-6', '2017-12-7', '2017-12-8', '2017-12-9', '2017-12-10', '2017-12-11', '2017-12-12', '2017-12-13', '2017-12-14', '2017-12-15', '2017-12-16', '2017-12-17', '2017-12-18', '2017-12-19', '2017-12-20', '2017-12-21', '2017-12-22', '2017-12-23', '2017-12-24', '2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28', '2017-12-29', '2017-12-30', '2017-12-31']
DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04',
'2017-12-05', '2017-12-06', '2017-12-07', '2017-12-08',
'2017-12-09', '2017-12-10', '2017-12-11', '2017-12-12',
'2017-12-13', '2017-12-14', '2017-12-15', '2017-12-16',
'2017-12-17', '2017-12-18', '2017-12-19', '2017-12-20',
'2017-12-21', '2017-12-22', '2017-12-23', '2017-12-24',
'2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
'2017-12-29', '2017-12-30', '2017-12-31'],
dtype='datetime64[ns]', freq=None)
月中日期为:
2017-12-17 00:00:00
f=open(r'C:\Users\HASEE\Desktop\pythontest\timetest','w')
stwrite='20150101,20150102,20150103,20150104,20150105,20150106,20150107,20150108'
f.write(stwrite)
f.close()
f=open(r'C:\Users\HASEE\Desktop\pythontest\timetest','r')
stread=f.read()
lst=stread.split(',')
print(lst)
print(pd.to_datetime(lst))
['20150101', '20150102', '20150103', '20150104', '20150105', '20150106', '20150107', '20150108']
DatetimeIndex(['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04',
'2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08'],
dtype='datetime64[ns]', freq=None)
'''
【课程2.10】 Pandas时间戳索引:DatetimeIndex
核心:pd.date_range()
'''
'\n【课程2.10】 Pandas时间戳索引:DatetimeIndex\n\n核心:pd.date_range()\n\n'
rng=pd.DatetimeIndex(['12/1/2017','12/2/2017','12/3/2017','12/4/2017','12/5/2017'])
print(rng,type(rng))
print(rng[0],type(rng[0]))
st=pd.Series(np.random.rand(len(rng)),index=rng)
print(st,type(st))
print(st.index)
DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04',
'2017-12-05'],
dtype='datetime64[ns]', freq=None)
2017-12-01 00:00:00
2017-12-01 0.745300
2017-12-02 0.876081
2017-12-03 0.142995
2017-12-04 0.938318
2017-12-05 0.043834
dtype: float64
DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04',
'2017-12-05'],
dtype='datetime64[ns]', freq=None)
rng1=pd.date_range(start='1/1/2017',end='1/10/2017', normalize=True)
rng2=pd.date_range(start='1/1/2017',periods=10)
rng3=pd.date_range(end = '1/30/2017 15:00:00', periods = 10)
print(rng1,type(rng1))
print(rng2)
print(rng3)
print('--------------')
rng4 = pd.date_range(start = '1/1/2017 15:30', periods = 10, name = 'hello world!', normalize = True)
print(rng4)
print('-------')
print(pd.date_range('20170101','20170104'))
print(pd.date_range('20170101','20170104',closed = 'right'))
print(pd.date_range('20170101','20170104',closed = 'left'))
print('-------')
print(pd.bdate_range('20170101','20170107'))
print(list(pd.date_range(start = '1/1/2017', periods = 10)))
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-01-21 15:00:00', '2017-01-22 15:00:00',
'2017-01-23 15:00:00', '2017-01-24 15:00:00',
'2017-01-25 15:00:00', '2017-01-26 15:00:00',
'2017-01-27 15:00:00', '2017-01-28 15:00:00',
'2017-01-29 15:00:00', '2017-01-30 15:00:00'],
dtype='datetime64[ns]', freq='D')
--------------
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10'],
dtype='datetime64[ns]', name='hello world!', freq='D')
-------
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'], dtype='datetime64[ns]', freq='D')
-------
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05',
'2017-01-06'],
dtype='datetime64[ns]', freq='B')
[Timestamp('2017-01-01 00:00:00', freq='D'), Timestamp('2017-01-02 00:00:00', freq='D'), Timestamp('2017-01-03 00:00:00', freq='D'), Timestamp('2017-01-04 00:00:00', freq='D'), Timestamp('2017-01-05 00:00:00', freq='D'), Timestamp('2017-01-06 00:00:00', freq='D'), Timestamp('2017-01-07 00:00:00', freq='D'), Timestamp('2017-01-08 00:00:00', freq='D'), Timestamp('2017-01-09 00:00:00', freq='D'), Timestamp('2017-01-10 00:00:00', freq='D')]
print(pd.date_range('2017/1/1','2017/1/4'))
print(pd.date_range('2017/1/1','2017/1/4', freq = 'B'))
print(pd.date_range('2017/1/1','2017/1/2', freq = 'H'))
print(pd.date_range('2017/1/1 12:00','2017/1/1 12:10', freq = 'T'))
print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10', freq = 'S'))
print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10', freq = 'L'))
print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10', freq = 'U'))
print(pd.date_range('20170101','20170201',freq='W-MON'))
print(pd.date_range('2017/1/1','2017/5/1',freq='wom-2mon'))
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='B')
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 01:00:00',
'2017-01-01 02:00:00', '2017-01-01 03:00:00',
'2017-01-01 04:00:00', '2017-01-01 05:00:00',
'2017-01-01 06:00:00', '2017-01-01 07:00:00',
'2017-01-01 08:00:00', '2017-01-01 09:00:00',
'2017-01-01 10:00:00', '2017-01-01 11:00:00',
'2017-01-01 12:00:00', '2017-01-01 13:00:00',
'2017-01-01 14:00:00', '2017-01-01 15:00:00',
'2017-01-01 16:00:00', '2017-01-01 17:00:00',
'2017-01-01 18:00:00', '2017-01-01 19:00:00',
'2017-01-01 20:00:00', '2017-01-01 21:00:00',
'2017-01-01 22:00:00', '2017-01-01 23:00:00',
'2017-01-02 00:00:00'],
dtype='datetime64[ns]', freq='H')
DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:01:00',
'2017-01-01 12:02:00', '2017-01-01 12:03:00',
'2017-01-01 12:04:00', '2017-01-01 12:05:00',
'2017-01-01 12:06:00', '2017-01-01 12:07:00',
'2017-01-01 12:08:00', '2017-01-01 12:09:00',
'2017-01-01 12:10:00'],
dtype='datetime64[ns]', freq='T')
DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:00:01',
'2017-01-01 12:00:02', '2017-01-01 12:00:03',
'2017-01-01 12:00:04', '2017-01-01 12:00:05',
'2017-01-01 12:00:06', '2017-01-01 12:00:07',
'2017-01-01 12:00:08', '2017-01-01 12:00:09',
'2017-01-01 12:00:10'],
dtype='datetime64[ns]', freq='S')
DatetimeIndex([ '2017-01-01 12:00:00', '2017-01-01 12:00:00.001000',
'2017-01-01 12:00:00.002000', '2017-01-01 12:00:00.003000',
'2017-01-01 12:00:00.004000', '2017-01-01 12:00:00.005000',
'2017-01-01 12:00:00.006000', '2017-01-01 12:00:00.007000',
'2017-01-01 12:00:00.008000', '2017-01-01 12:00:00.009000',
...
'2017-01-01 12:00:09.991000', '2017-01-01 12:00:09.992000',
'2017-01-01 12:00:09.993000', '2017-01-01 12:00:09.994000',
'2017-01-01 12:00:09.995000', '2017-01-01 12:00:09.996000',
'2017-01-01 12:00:09.997000', '2017-01-01 12:00:09.998000',
'2017-01-01 12:00:09.999000', '2017-01-01 12:00:10'],
dtype='datetime64[ns]', length=10001, freq='L')
DatetimeIndex([ '2017-01-01 12:00:00', '2017-01-01 12:00:00.000001',
'2017-01-01 12:00:00.000002', '2017-01-01 12:00:00.000003',
'2017-01-01 12:00:00.000004', '2017-01-01 12:00:00.000005',
'2017-01-01 12:00:00.000006', '2017-01-01 12:00:00.000007',
'2017-01-01 12:00:00.000008', '2017-01-01 12:00:00.000009',
...
'2017-01-01 12:00:09.999991', '2017-01-01 12:00:09.999992',
'2017-01-01 12:00:09.999993', '2017-01-01 12:00:09.999994',
'2017-01-01 12:00:09.999995', '2017-01-01 12:00:09.999996',
'2017-01-01 12:00:09.999997', '2017-01-01 12:00:09.999998',
'2017-01-01 12:00:09.999999', '2017-01-01 12:00:10'],
dtype='datetime64[ns]', length=10000001, freq='U')
DatetimeIndex(['2017-01-02', '2017-01-09', '2017-01-16', '2017-01-23',
'2017-01-30'],
dtype='datetime64[ns]', freq='W-MON')
DatetimeIndex(['2017-01-09', '2017-02-13', '2017-03-13', '2017-04-10'], dtype='datetime64[ns]', freq='WOM-2MON')
print(pd.date_range('2017','2018', freq = 'M'))
print(pd.date_range('2017','2020', freq = 'Q-DEC'))
print(pd.date_range('2017','2020', freq = 'A-DEC'))
print('------')
print(pd.date_range('2017','2018', freq = 'BM'))
print(pd.date_range('2017','2020', freq = 'BQ-DEC'))
print(pd.date_range('2017','2020', freq = 'BA-DEC'))
print('------')
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'],
dtype='datetime64[ns]', freq='M')
DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-30', '2017-12-31',
'2018-03-31', '2018-06-30', '2018-09-30', '2018-12-31',
'2019-03-31', '2019-06-30', '2019-09-30', '2019-12-31'],
dtype='datetime64[ns]', freq='Q-DEC')
DatetimeIndex(['2017-12-31', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='A-DEC')
------
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-28',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-29', '2017-10-31', '2017-11-30', '2017-12-29'],
dtype='datetime64[ns]', freq='BM')
DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-29', '2017-12-29',
'2018-03-30', '2018-06-29', '2018-09-28', '2018-12-31',
'2019-03-29', '2019-06-28', '2019-09-30', '2019-12-31'],
dtype='datetime64[ns]', freq='BQ-DEC')
DatetimeIndex(['2017-12-29', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='BA-DEC')
------
print(pd.date_range('2017/1/1','2017/2/1', freq = '7D'))
print(pd.date_range('2017/1/1','2017/1/2', freq = '2h30min'))
print(pd.date_range('2017','2018', freq = '2M'))
DatetimeIndex(['2017-01-01', '2017-01-08', '2017-01-15', '2017-01-22',
'2017-01-29'],
dtype='datetime64[ns]', freq='7D')
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 02:30:00',
'2017-01-01 05:00:00', '2017-01-01 07:30:00',
'2017-01-01 10:00:00', '2017-01-01 12:30:00',
'2017-01-01 15:00:00', '2017-01-01 17:30:00',
'2017-01-01 20:00:00', '2017-01-01 22:30:00'],
dtype='datetime64[ns]', freq='150T')
DatetimeIndex(['2017-01-31', '2017-03-31', '2017-05-31', '2017-07-31',
'2017-09-30', '2017-11-30'],
dtype='datetime64[ns]', freq='2M')
ts=pd.Series(np.random.rand(4),
index=pd.date_range('20170101','20170104'))
print(ts)
print(ts.asfreq('4h',method='ffill'))
2017-01-01 0.435837
2017-01-02 0.669417
2017-01-03 0.198081
2017-01-04 0.139088
Freq: D, dtype: float64
2017-01-01 00:00:00 0.435837
2017-01-01 04:00:00 0.435837
2017-01-01 08:00:00 0.435837
2017-01-01 12:00:00 0.435837
2017-01-01 16:00:00 0.435837
2017-01-01 20:00:00 0.435837
2017-01-02 00:00:00 0.669417
2017-01-02 04:00:00 0.669417
2017-01-02 08:00:00 0.669417
2017-01-02 12:00:00 0.669417
2017-01-02 16:00:00 0.669417
2017-01-02 20:00:00 0.669417
2017-01-03 00:00:00 0.198081
2017-01-03 04:00:00 0.198081
2017-01-03 08:00:00 0.198081
2017-01-03 12:00:00 0.198081
2017-01-03 16:00:00 0.198081
2017-01-03 20:00:00 0.198081
2017-01-04 00:00:00 0.139088
Freq: 4H, dtype: float64
ts = pd.Series(np.random.rand(4),
index = pd.date_range('20170101','20170104'))
print(ts)
print(ts.shift(2))
print(ts.shift(-2))
print('------')
per = ts/ts.shift(1) - 1
print(per)
print('------')
print(ts.shift(2, freq = 'D'))
print(ts.shift(2, freq = 'T'))
2017-01-01 0.696547
2017-01-02 0.529174
2017-01-03 0.404338
2017-01-04 0.741592
Freq: D, dtype: float64
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 0.696547
2017-01-04 0.529174
Freq: D, dtype: float64
2017-01-01 0.404338
2017-01-02 0.741592
2017-01-03 NaN
2017-01-04 NaN
Freq: D, dtype: float64
------
2017-01-01 NaN
2017-01-02 -0.240289
2017-01-03 -0.235907
2017-01-04 0.834088
Freq: D, dtype: float64
------
2017-01-03 0.696547
2017-01-04 0.529174
2017-01-05 0.404338
2017-01-06 0.741592
Freq: D, dtype: float64
2017-01-01 00:02:00 0.696547
2017-01-02 00:02:00 0.529174
2017-01-03 00:02:00 0.404338
2017-01-04 00:02:00 0.741592
Freq: D, dtype: float64
di1=pd.date_range(start='2017-01',periods=5,freq='D')
ts1=pd.Series(data=np.random.rand(5),index=di1)
print(ts1)
di2=pd.date_range(start='2017-01-31',periods=5,freq='Q-Jan')
ts2=pd.Series(data=np.random.rand(5),index=di2)
print(ts2)
di3=pd.date_range(start='2017-12-01',periods=4,freq='10t')
tdf=pd.DataFrame(data=np.random.rand(4,4),
columns=['value1','value2','value3','value4'],
index=di3
)
print(tdf)
2017-01-01 0.324245
2017-01-02 0.115597
2017-01-03 0.407691
2017-01-04 0.996330
2017-01-05 0.495723
Freq: D, dtype: float64
2017-01-31 0.650247
2017-04-30 0.484225
2017-07-31 0.464591
2017-10-31 0.641705
2018-01-31 0.795587
Freq: Q-JAN, dtype: float64
value1 value2 value3 value4
2017-12-01 00:00:00 0.494249 0.925107 0.769139 0.950450
2017-12-01 00:10:00 0.170856 0.021507 0.119379 0.391953
2017-12-01 00:20:00 0.848345 0.312652 0.699692 0.931733
2017-12-01 00:30:00 0.559365 0.542697 0.125891 0.055697
di1=pd.date_range(start='2017 05 01 12',periods=5,freq='10min')
ts1=pd.Series(data=np.random.rand(5),index=di1)
print(ts1)
ts2=ts1.asfreq(freq='5t',method='ffill')
print(ts2)
2017-05-01 12:00:00 0.478802
2017-05-01 12:10:00 0.290849
2017-05-01 12:20:00 0.895803
2017-05-01 12:30:00 0.109338
2017-05-01 12:40:00 0.697874
Freq: 10T, dtype: float64
2017-05-01 12:00:00 0.478802
2017-05-01 12:05:00 0.478802
2017-05-01 12:10:00 0.290849
2017-05-01 12:15:00 0.290849
2017-05-01 12:20:00 0.895803
2017-05-01 12:25:00 0.895803
2017-05-01 12:30:00 0.109338
2017-05-01 12:35:00 0.109338
2017-05-01 12:40:00 0.697874
Freq: 5T, dtype: float64
'''
【课程2.11】 Pandas时期:Period 和TimeStamp不同
Period代表时期
TimeStamp代表时间点
核心:pd.Period()
pd.period_range()
'''
'\n【课程2.11】 Pandas时期:Period 和TimeStamp不同 \nPeriod代表时期 \nTimeStamp代表时间点\n\n\n核心:pd.Period()\n pd.period_range()\n\n'
p = pd.Period('2017',freq='m')
print(p,type(p))
print(p+1)
print(p-2)
print(pd.Period('2012',freq='a')-1)
2017-01
2017-02
2016-11
2011
prng=pd.period_range('20110101','20120101',freq='m')
print(prng,type(prng))
print(prng[0],type(prng[0]))
ts = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts,type(ts))
print(ts.index)
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
'2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
'2012-01'],
dtype='period[M]', freq='M')
2011-01
2011-01 0.270300
2011-02 0.528558
2011-03 0.626401
2011-04 0.812753
2011-05 0.353819
2011-06 0.150295
2011-07 0.461801
2011-08 0.533077
2011-09 0.054538
2011-10 0.833580
2011-11 0.894351
2011-12 0.383080
2012-01 0.181327
Freq: M, dtype: float64
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
'2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
'2012-01'],
dtype='period[M]', freq='M')
p = pd.Period('2017','A')
print(p)
print(p.asfreq('M', how = 'start'))
print(p.asfreq('D', how = 'end'))
print('--------')
prng = pd.period_range('2017','2018',freq = 'M')
ts1 = pd.Series(np.random.rand(len(prng)), index = prng)
ts2 = pd.Series(np.random.rand(len(prng)), index = prng.asfreq('D', how = 'start'))
print(ts1.head(),len(ts1))
print(ts2.head(),len(ts2))
2017
2017-01
2017-12-31
--------
2017-01 0.404408
2017-02 0.064027
2017-03 0.821275
2017-04 0.995918
2017-05 0.281146
Freq: M, dtype: float64 13
2017-01-01 0.920413
2017-02-01 0.320414
2017-03-01 0.847685
2017-04-01 0.261885
2017-05-01 0.145836
Freq: D, dtype: float64 13
rng = pd.date_range('2017/1/1', periods = 10, freq = 'M')
prng = pd.period_range('2017','2018', freq = 'M')
print(rng)
print(prng)
print('----------')
print(rng.to_period(freq='a'))
ts1 = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts1)
print(ts1.to_period())
ts2 = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts2)
print(ts2.to_timestamp(how='S'))
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-30', '2017-10-31'],
dtype='datetime64[ns]', freq='M')
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',
'2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',
'2018-01'],
dtype='period[M]', freq='M')
----------
PeriodIndex(['2017', '2017', '2017', '2017', '2017', '2017', '2017', '2017',
'2017', '2017'],
dtype='period[A-DEC]', freq='A-DEC')
2017-01-31 0.079345
2017-02-28 0.699806
2017-03-31 0.193150
2017-04-30 0.380251
2017-05-31 0.218401
2017-06-30 0.282242
2017-07-31 0.450215
2017-08-31 0.708842
2017-09-30 0.928040
2017-10-31 0.697656
Freq: M, dtype: float64
2017-01 0.079345
2017-02 0.699806
2017-03 0.193150
2017-04 0.380251
2017-05 0.218401
2017-06 0.282242
2017-07 0.450215
2017-08 0.708842
2017-09 0.928040
2017-10 0.697656
Freq: M, dtype: float64
2017-01 0.543350
2017-02 0.787547
2017-03 0.842127
2017-04 0.020524
2017-05 0.981086
2017-06 0.391997
2017-07 0.639751
2017-08 0.654858
2017-09 0.571412
2017-10 0.136572
2017-11 0.537329
2017-12 0.834277
2018-01 0.497881
Freq: M, dtype: float64
2017-01-01 0.543350
2017-02-01 0.787547
2017-03-01 0.842127
2017-04-01 0.020524
2017-05-01 0.981086
2017-06-01 0.391997
2017-07-01 0.639751
2017-08-01 0.654858
2017-09-01 0.571412
2017-10-01 0.136572
2017-11-01 0.537329
2017-12-01 0.834277
2018-01-01 0.497881
Freq: MS, dtype: float64
p1=pd.period_range(start='2017-01',end='2017-05',freq='m')
ts1=pd.Series(data=np.random.rand(5),index=p1)
print(ts1)
p2=pd.period_range(start='2017-01-1',periods=5,freq='2h')
ts2=pd.Series(data=np.random.rand(5),index=p2)
print(ts2)
2017-01 0.884941
2017-02 0.499193
2017-03 0.157167
2017-04 0.964902
2017-05 0.059249
Freq: M, dtype: float64
2017-01-01 00:00 0.865604
2017-01-01 02:00 0.262509
2017-01-01 04:00 0.087934
2017-01-01 06:00 0.834944
2017-01-01 08:00 0.915583
Freq: 2H, dtype: float64
'''
#总结2.11
1 p = pd.Period('2017',freq='m')
创建单个period
2 prng=pd.period_range('20110101','20120101',freq='m')
创建list of period
3p = pd.Period('2017','A')
print(p)
print(p.asfreq('M', how = 'start'))
更改period的频率
4pd.to_period()、pd.to_timestamp()
timestamp和period之间的转换
既可以直接在 listoftimestamp 和 listofperiods 之间转换
也可以直接对ts进行转换
'''
"\n#总结2.11\n1 p = pd.Period('2017',freq='m')\n创建单个period\n\n2 prng=pd.period_range('20110101','20120101',freq='m')\n创建list of period\n\n3p = pd.Period('2017','A')\nprint(p)\nprint(p.asfreq('M', how = 'start')) \n更改period的频率\n\n4pd.to_period()、pd.to_timestamp() \ntimestamp和period之间的转换\n既可以直接在 listoftimestamp 和 listofperiods 之间转换\n也可以直接对ts进行转换\n\n\n"
'''
【课程2.12】 时间序列 - 索引及切片
TimeSeries是Series的一个子类,所以Series索引及数据选取方面的方法基本一样
同时TimeSeries通过时间序列有更便捷的方法做索引和切片
'''
'\n【课程2.12】 时间序列 - 索引及切片\n\nTimeSeries是Series的一个子类,所以Series索引及数据选取方面的方法基本一样\n\n同时TimeSeries通过时间序列有更便捷的方法做索引和切片\n \n'
from datetime import datetime
rng=pd.date_range('2017/1','2017/3')
ts=pd.Series(np.random.rand(len(rng)),index=rng)
print(ts.head())
print(ts[0])
print(ts[:2])
print('-----')
print(ts['2017/1/2'])
print(ts['20170103'])
print(ts['1/10/2017'])
print(ts[datetime(2017,1,20)])
print('-----')
2017-01-01 0.659568
2017-01-02 0.151966
2017-01-03 0.288654
2017-01-04 0.583045
2017-01-05 0.769449
Freq: D, dtype: float64
0.65956785928946
2017-01-01 0.659568
2017-01-02 0.151966
Freq: D, dtype: float64
-----
0.15196600248594228
0.2886538761520884
0.0023193408918270597
0.20779042212900634
-----
rng = pd.date_range('2017/1','2017/3',freq = '12H')
ts = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts['2017/1/5':'2017/1/10'])
print('-----')
print(ts['2017/2'].head())
2017-01-05 00:00:00 0.318971
2017-01-05 12:00:00 0.188438
2017-01-06 00:00:00 0.187238
2017-01-06 12:00:00 0.980087
2017-01-07 00:00:00 0.161376
2017-01-07 12:00:00 0.215822
2017-01-08 00:00:00 0.935772
2017-01-08 12:00:00 0.880521
2017-01-09 00:00:00 0.168200
2017-01-09 12:00:00 0.276908
2017-01-10 00:00:00 0.795692
2017-01-10 12:00:00 0.096272
Freq: 12H, dtype: float64
-----
2017-02-01 00:00:00 0.536472
2017-02-01 12:00:00 0.063015
2017-02-02 00:00:00 0.159657
2017-02-02 12:00:00 0.614637
2017-02-03 00:00:00 0.984499
Freq: 12H, dtype: float64
dates = pd.DatetimeIndex(['1/1/2015','1/2/2015','1/3/2015','1/4/2015','1/1/2015','1/2/2015'])
ts = pd.Series(np.random.rand(6), index = dates)
print(ts)
print(ts.is_unique,ts.index.is_unique)
print('-----')
print(ts['20150101'],type(ts['20150101']))
print(ts['20150104'],type(ts['20150104']))
print('-----')
print(ts.groupby(level=0).mean)
2015-01-01 0.510849
2015-01-02 0.791319
2015-01-03 0.552804
2015-01-04 0.678201
2015-01-01 0.571113
2015-01-02 0.584924
dtype: float64
True False
-----
2015-01-01 0.510849
2015-01-01 0.571113
dtype: float64
2015-01-04 0.678201
dtype: float64
-----
>
di=pd.date_range(start='2017/12/1',end='2017/12/5 12:00:00',freq='12H')
ts=pd.DataFrame(data=np.random.rand(10,3),index=di,columns=['value1','value2','value3'])
print(ts)
print('---------------')
print(ts.iloc[0:3])
print('---------------')
print(ts.loc['2017-12-4 12:00:00'])
print('---------------')
print(ts.loc['2017-12-4' : '2017-12-5'])
value1 value2 value3
2017-12-01 00:00:00 0.134941 0.767928 0.157234
2017-12-01 12:00:00 0.869153 0.842300 0.358291
2017-12-02 00:00:00 0.728543 0.706397 0.737571
2017-12-02 12:00:00 0.433480 0.464147 0.987012
2017-12-03 00:00:00 0.196458 0.474911 0.709055
2017-12-03 12:00:00 0.076818 0.907870 0.886325
2017-12-04 00:00:00 0.784369 0.985650 0.652622
2017-12-04 12:00:00 0.058532 0.784861 0.318300
2017-12-05 00:00:00 0.943825 0.025305 0.848250
2017-12-05 12:00:00 0.636666 0.767742 0.773654
---------------
value1 value2 value3
2017-12-01 00:00:00 0.134941 0.767928 0.157234
2017-12-01 12:00:00 0.869153 0.842300 0.358291
2017-12-02 00:00:00 0.728543 0.706397 0.737571
---------------
value1 0.058532
value2 0.784861
value3 0.318300
Name: 2017-12-04 12:00:00, dtype: float64
---------------
value1 value2 value3
2017-12-04 00:00:00 0.784369 0.985650 0.652622
2017-12-04 12:00:00 0.058532 0.784861 0.318300
2017-12-05 00:00:00 0.943825 0.025305 0.848250
2017-12-05 12:00:00 0.636666 0.767742 0.773654
'''
#总结2.12
1 ts的索引
2 ts的切片
3 ts判断是否重复(data index)
'''
'\n#总结2.12\n1 ts的索引\n2 ts的切片\n3 ts判断是否重复(data index)\n'
rng = pd.date_range('20170101', periods = 12)
ts = pd.Series(np.arange(12), index = rng)
print(ts)
print('-----')
ts_re = ts.resample('5D')
ts_re2 = ts.resample('5D').sum()
print(ts_re, type(ts_re))
print('-----')
print(ts_re2, type(ts_re2))
print('-----')
2017-01-01 0
2017-01-02 1
2017-01-03 2
2017-01-04 3
2017-01-05 4
2017-01-06 5
2017-01-07 6
2017-01-08 7
2017-01-09 8
2017-01-10 9
2017-01-11 10
2017-01-12 11
Freq: D, dtype: int32
-----
DatetimeIndexResampler [freq=<5 * Days>, axis=0, closed=left, label=left, convention=start, base=0]
-----
2017-01-01 10
2017-01-06 35
2017-01-11 21
Freq: 5D, dtype: int32
-----
rng=pd.date_range('20170101',periods=12)
ts=pd.Series(np.arange(1,13),index=rng)
print(ts)
print('---------')
print(ts.resample('5D').sum(),'→ 默认\n')
print(ts.resample('5D', closed = 'left').sum(),'→ left\n')
print(ts.resample('5D', closed = 'right').sum(),'→ right\n')
print('-----')
print(ts.resample('5D', label = 'left').sum(),'→ leftlabel\n')
print(ts.resample('5D', label = 'right').sum(),'→ rightlabel\n')
2017-01-01 1
2017-01-02 2
2017-01-03 3
2017-01-04 4
2017-01-05 5
2017-01-06 6
2017-01-07 7
2017-01-08 8
2017-01-09 9
2017-01-10 10
2017-01-11 11
2017-01-12 12
Freq: D, dtype: int32
---------
2017-01-01 15
2017-01-06 40
2017-01-11 23
Freq: 5D, dtype: int32 → 默认
2017-01-01 15
2017-01-06 40
2017-01-11 23
Freq: 5D, dtype: int32 → left
2016-12-27 1
2017-01-01 20
2017-01-06 45
2017-01-11 12
Freq: 5D, dtype: int32 → right
-----
2017-01-01 15
2017-01-06 40
2017-01-11 23
Freq: 5D, dtype: int32 → leftlabel
2017-01-06 15
2017-01-11 40
2017-01-16 23
Freq: 5D, dtype: int32 → rightlabel
rng = pd.date_range('2017/1/1 0:0:0', periods = 5, freq = 'H')
ts = pd.DataFrame(np.arange(15).reshape(5,3),
index = rng,
columns = ['a','b','c'])
print(ts)
print('---------')
print(ts.resample('15T').asfreq())
print(ts.resample('15T').ffill())
print(ts.resample('15T').bfill())
a b c
2017-01-01 00:00:00 0 1 2
2017-01-01 01:00:00 3 4 5
2017-01-01 02:00:00 6 7 8
2017-01-01 03:00:00 9 10 11
2017-01-01 04:00:00 12 13 14
---------
a b c
2017-01-01 00:00:00 0.0 1.0 2.0
2017-01-01 00:15:00 NaN NaN NaN
2017-01-01 00:30:00 NaN NaN NaN
2017-01-01 00:45:00 NaN NaN NaN
2017-01-01 01:00:00 3.0 4.0 5.0
2017-01-01 01:15:00 NaN NaN NaN
2017-01-01 01:30:00 NaN NaN NaN
2017-01-01 01:45:00 NaN NaN NaN
2017-01-01 02:00:00 6.0 7.0 8.0
2017-01-01 02:15:00 NaN NaN NaN
2017-01-01 02:30:00 NaN NaN NaN
2017-01-01 02:45:00 NaN NaN NaN
2017-01-01 03:00:00 9.0 10.0 11.0
2017-01-01 03:15:00 NaN NaN NaN
2017-01-01 03:30:00 NaN NaN NaN
2017-01-01 03:45:00 NaN NaN NaN
2017-01-01 04:00:00 12.0 13.0 14.0
a b c
2017-01-01 00:00:00 0 1 2
2017-01-01 00:15:00 0 1 2
2017-01-01 00:30:00 0 1 2
2017-01-01 00:45:00 0 1 2
2017-01-01 01:00:00 3 4 5
2017-01-01 01:15:00 3 4 5
2017-01-01 01:30:00 3 4 5
2017-01-01 01:45:00 3 4 5
2017-01-01 02:00:00 6 7 8
2017-01-01 02:15:00 6 7 8
2017-01-01 02:30:00 6 7 8
2017-01-01 02:45:00 6 7 8
2017-01-01 03:00:00 9 10 11
2017-01-01 03:15:00 9 10 11
2017-01-01 03:30:00 9 10 11
2017-01-01 03:45:00 9 10 11
2017-01-01 04:00:00 12 13 14
a b c
2017-01-01 00:00:00 0 1 2
2017-01-01 00:15:00 3 4 5
2017-01-01 00:30:00 3 4 5
2017-01-01 00:45:00 3 4 5
2017-01-01 01:00:00 3 4 5
2017-01-01 01:15:00 6 7 8
2017-01-01 01:30:00 6 7 8
2017-01-01 01:45:00 6 7 8
2017-01-01 02:00:00 6 7 8
2017-01-01 02:15:00 9 10 11
2017-01-01 02:30:00 9 10 11
2017-01-01 02:45:00 9 10 11
2017-01-01 03:00:00 9 10 11
2017-01-01 03:15:00 12 13 14
2017-01-01 03:30:00 12 13 14
2017-01-01 03:45:00 12 13 14
2017-01-01 04:00:00 12 13 14
di=pd.date_range(start='20170101',end='20170110')
ts1=pd.Series(data=np.random.rand(10),index=di)
print(ts1)
print('---------')
ts2=ts1.resample('3D',label='right',closed='right').mean()
print(ts2)
print('---------')
ts3=ts1.resample('12H').ffill()
print(ts3)
2017-01-01 0.528825
2017-01-02 0.595066
2017-01-03 0.721156
2017-01-04 0.637974
2017-01-05 0.449776
2017-01-06 0.430048
2017-01-07 0.256290
2017-01-08 0.493641
2017-01-09 0.272481
2017-01-10 0.878957
Freq: D, dtype: float64
---------
2017-01-01 0.528825
2017-01-04 0.651399
2017-01-07 0.378704
2017-01-10 0.548360
Freq: 3D, dtype: float64
---------
2017-01-01 00:00:00 0.528825
2017-01-01 12:00:00 0.528825
2017-01-02 00:00:00 0.595066
2017-01-02 12:00:00 0.595066
2017-01-03 00:00:00 0.721156
2017-01-03 12:00:00 0.721156
2017-01-04 00:00:00 0.637974
2017-01-04 12:00:00 0.637974
2017-01-05 00:00:00 0.449776
2017-01-05 12:00:00 0.449776
2017-01-06 00:00:00 0.430048
2017-01-06 12:00:00 0.430048
2017-01-07 00:00:00 0.256290
2017-01-07 12:00:00 0.256290
2017-01-08 00:00:00 0.493641
2017-01-08 12:00:00 0.493641
2017-01-09 00:00:00 0.272481
2017-01-09 12:00:00 0.272481
2017-01-10 00:00:00 0.878957
Freq: 12H, dtype: float64
'''
总结
1 ts降采样
2 ts升采样
'''
'\n总结\n1 ts降采样\n2 ts升采样\n'