from datetime import datetime
from datetime import timedelta
import pandas as pd
import numpy as np
date1 = datetime(2016,6,3)
date2 = datetime(2015,6,3)
delta = date1-date2
delta.days
366
delta.total_seconds()
31622400.0
str(date1)
'2016-06-03 00:00:00'
import time
dir(time)
['_STRUCT_TM_ITEMS',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'altzone',
'asctime',
'clock',
'ctime',
'daylight',
'get_clock_info',
'gmtime',
'localtime',
'mktime',
'monotonic',
'perf_counter',
'process_time',
'sleep',
'strftime',
'strptime',
'struct_time',
'time',
'timezone',
'tzname']
datetime.strptime('2016-03-20 09:30','%Y-%m-%d %H:%M')
datetime.datetime(2016, 3, 20, 9, 30)
data_list=list(pd.date_range('20160320','20160331'))
for i in data_list:
print(i)
2016-03-20 00:00:00
2016-03-21 00:00:00
2016-03-22 00:00:00
2016-03-23 00:00:00
2016-03-24 00:00:00
2016-03-25 00:00:00
2016-03-26 00:00:00
2016-03-27 00:00:00
2016-03-28 00:00:00
2016-03-29 00:00:00
2016-03-30 00:00:00
2016-03-31 00:00:00
pd.date_range(start='20160320',periods=10)
DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',
'2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',
'2016-03-28', '2016-03-29'],
dtype='datetime64[ns]', freq='D')
#时间频率
pd.date_range(start='20190305',periods=10,freq='M')
DatetimeIndex(['2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30',
'2019-07-31', '2019-08-31', '2019-09-30', '2019-10-31',
'2019-11-30', '2019-12-31'],
dtype='datetime64[ns]', freq='M')
p1 = pd.Period(2010)
p1
Period('2010', 'A-DEC')
p1+2
Period('2012', 'A-DEC')
#时期频率的转换
p = pd.Period('2016',freq="M")
p
Period('2016-01', 'M')
p.asfreq('4H',how='start')
Period('2016-01-01 00:00', '4H')
##重采样
ts = pd.Series(np.random.randint(0,50,60),index=pd.date_range('2016-04-03 09:30',periods=60,freq='T'))
ts
2016-04-03 09:30:00 7
2016-04-03 09:31:00 42
2016-04-03 09:32:00 23
2016-04-03 09:33:00 38
2016-04-03 09:34:00 15
2016-04-03 09:35:00 39
2016-04-03 09:36:00 14
2016-04-03 09:37:00 18
2016-04-03 09:38:00 15
2016-04-03 09:39:00 9
2016-04-03 09:40:00 29
2016-04-03 09:41:00 2
2016-04-03 09:42:00 6
2016-04-03 09:43:00 6
2016-04-03 09:44:00 20
2016-04-03 09:45:00 3
2016-04-03 09:46:00 7
2016-04-03 09:47:00 16
2016-04-03 09:48:00 37
2016-04-03 09:49:00 21
2016-04-03 09:50:00 24
2016-04-03 09:51:00 30
2016-04-03 09:52:00 27
2016-04-03 09:53:00 1
2016-04-03 09:54:00 18
2016-04-03 09:55:00 3
2016-04-03 09:56:00 5
2016-04-03 09:57:00 43
2016-04-03 09:58:00 2
2016-04-03 09:59:00 27
2016-04-03 10:00:00 5
2016-04-03 10:01:00 32
2016-04-03 10:02:00 38
2016-04-03 10:03:00 37
2016-04-03 10:04:00 17
2016-04-03 10:05:00 27
2016-04-03 10:06:00 37
2016-04-03 10:07:00 4
2016-04-03 10:08:00 27
2016-04-03 10:09:00 32
2016-04-03 10:10:00 1
2016-04-03 10:11:00 49
2016-04-03 10:12:00 15
2016-04-03 10:13:00 30
2016-04-03 10:14:00 22
2016-04-03 10:15:00 19
2016-04-03 10:16:00 21
2016-04-03 10:17:00 46
2016-04-03 10:18:00 29
2016-04-03 10:19:00 5
2016-04-03 10:20:00 37
2016-04-03 10:21:00 0
2016-04-03 10:22:00 10
2016-04-03 10:23:00 29
2016-04-03 10:24:00 16
2016-04-03 10:25:00 32
2016-04-03 10:26:00 34
2016-04-03 10:27:00 6
2016-04-03 10:28:00 17
2016-04-03 10:29:00 36
Freq: T, dtype: int32
ts.resample('4min',how='sum')
C:\Program Files\Anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated
the new Syntax is .resample(...).sum()
if __name__ == '__main__':
2016-04-03 09:28:00 49
2016-04-03 09:32:00 115
2016-04-03 09:36:00 56
2016-04-03 09:40:00 43
2016-04-03 09:44:00 46
2016-04-03 09:48:00 112
2016-04-03 09:52:00 49
2016-04-03 09:56:00 77
2016-04-03 10:00:00 112
2016-04-03 10:04:00 85
2016-04-03 10:08:00 109
2016-04-03 10:12:00 86
2016-04-03 10:16:00 101
2016-04-03 10:20:00 76
2016-04-03 10:24:00 88
2016-04-03 10:28:00 53
Freq: 4T, dtype: int32
ts.resample('5min',how='sum',label='right')
C:\Program Files\Anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: how in .resample() is deprecated
the new Syntax is .resample(...).sum()
if __name__ == '__main__':
2016-04-03 09:35:00 125
2016-04-03 09:40:00 95
2016-04-03 09:45:00 63
2016-04-03 09:50:00 84
2016-04-03 09:55:00 100
2016-04-03 10:00:00 80
2016-04-03 10:05:00 129
2016-04-03 10:10:00 127
2016-04-03 10:15:00 117
2016-04-03 10:20:00 120
2016-04-03 10:25:00 92
2016-04-03 10:30:00 125
Freq: 5T, dtype: int32
#金融数据专用 Open/High/Close
ts.resample('5min',how='ohlc')
C:\Program Files\Anaconda\lib\site-packages\ipykernel\__main__.py:2: FutureWarning: how in .resample() is deprecated
the new Syntax is .resample(...).ohlc()
from ipykernel import kernelapp as app
open | high | low | close | |
---|---|---|---|---|
2016-04-03 09:30:00 | 7 | 42 | 7 | 15 |
2016-04-03 09:35:00 | 39 | 39 | 9 | 9 |
2016-04-03 09:40:00 | 29 | 29 | 2 | 20 |
2016-04-03 09:45:00 | 3 | 37 | 3 | 21 |
2016-04-03 09:50:00 | 24 | 30 | 1 | 18 |
2016-04-03 09:55:00 | 3 | 43 | 2 | 27 |
2016-04-03 10:00:00 | 5 | 38 | 5 | 17 |
2016-04-03 10:05:00 | 27 | 37 | 4 | 32 |
2016-04-03 10:10:00 | 1 | 49 | 1 | 22 |
2016-04-03 10:15:00 | 19 | 46 | 5 | 5 |
2016-04-03 10:20:00 | 37 | 37 | 0 | 16 |
2016-04-03 10:25:00 | 32 | 36 | 6 | 36 |
#通过groupby(lambda x: x.month).sum()
ts.groupby(lambda x:x.month).sum()
4 1257
dtype: int32
df.resample('D',fill_method='ffill',limit=3)
df.resample('W-MON',fill_method='ffill')#一周为单位,每周一采样;fill_method表示填充
#从文件读取日期序列
df = pd.read_csv('data/002001.csv',index_col='Date')
'''
parse_dates=True #自动对时间进行解析
date_parset=函数名 #自定义时间日期解析函数
'''
def date_parser(s):
s = '2016/' + s
d = datetime.strptime(s, '%Y/%m/%d')
return d
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。