Numpy中的时间类型
从Numpy1.7开始,已经有了原生的日期-时间支持,基本类型称为datetime64。
In [1]: import numpy as np In [2]: nd = np.datetime64('2018-05-08') In [3]: nd Out[3]: numpy.datetime64('2018-05-08')
和datetime一样,datetime64对象可以表示为字符串对象。
In [4]: np.datetime_as_string(nd) Out[4]: '2018-05-08'
每个这类对象都保存在元数据,可以通过date_time_date方法访问,两个主要的组成部分是频率信息和单位(D:天)。
In [5]: np.datetime_data(nd) Out[5]: ('D', 1)
从datetime对象中构造datetime64对象:
In [7]: d = datetime.datetime(2018,7,31,12,16,30,500000) In [8]: d Out[8]: datetime.datetime(2018, 7, 31, 12, 16, 30, 500000) In [10]: ng = np.datetime64(d) In [11]: ng Out[11]: numpy.datetime64('2018-07-31T12:16:30.500000')
同样,可以将datetime64对象转换成datetime对象:
In [12]: nd.astype(datetime.datetime) Out[12]: datetime.date(2018, 5, 8) In [13]: nd.astype(datetime.date) Out[13]: datetime.date(2018, 5, 8)
另外还有另外一种不常见的datetime64的构造方法:(提供一个字符串解和频率信息)
In [15]: nh = np.datetime64('2015-10','D') In [16]: nh Out[16]: numpy.datetime64('2015-10-01')
使用arange函数生成一个日期范围,当然可以指定日期范围
In [17]: np.array(['2015-10-01','2015-10-05','2015-10-10'],dtype='datetime64') Out[17]: array(['2015-10-01', '2015-10-05', '2015-10-10'], dtype='datetime64[D]') In [18]: np.arange('2018-10-01','2018-10-08',dtype='datetime64') Out[18]: array(['2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07'], dtype='datetime64[D]') In [19]: np.arange('2018-10-01','2018-10-08',dtype='datetime64[D]') Out[19]: array(['2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07'], dtype='datetime64[D]') In [20]: np.arange('2018-10-01','2018-10-31',dtype='datetime64[W]') Out[20]: array(['2018-09-27', '2018-10-04', '2018-10-11', '2018-10-18'], dtype='datetime64[W]') In [21]: dt1 = np.arange('2018-01-01 10:10:10','2018-01-02 10:10:10',dtype='datetime64[h]')[:10] In [22]: dt1 Out[22]: array(['2018-01-01T10', '2018-01-01T11', '2018-01-01T12', '2018-01-01T13', '2018-01-01T14', '2018-01-01T15', '2018-01-01T16', '2018-01-01T17', '2018-01-01T18', '2018-01-01T19'], dtype='datetime64[h]')
In [36]: np.arange('2016-01-01T00:00:00','2016-01-02T00:00:00',dtype='datetime64[s]')[:20] Out[36]: array(['2016-01-01T00:00:00', '2016-01-01T00:00:01', '2016-01-01T00:00:02', '2016-01-01T00:00:03', '2016-01-01T00:00:04', '2016-01-01T00:00:05', '2016-01-01T00:00:06', '2016-01-01T00:00:07', '2016-01-01T00:00:08', '2016-01-01T00:00:09', '2016-01-01T00:00:10', '2016-01-01T00:00:11', '2016-01-01T00:00:12', '2016-01-01T00:00:13', '2016-01-01T00:00:14', '2016-01-01T00:00:15', '2016-01-01T00:00:16', '2016-01-01T00:00:17', '2016-01-01T00:00:18', '2016-01-01T00:00:19'], dtype='datetime64[s]') In [37]: np.arange('2016-01-01T00:00:00','2016-01-02T00:00:00',dtype='datetime64[ms]')[:20] Out[37]: array(['2016-01-01T00:00:00.000', '2016-01-01T00:00:00.001', '2016-01-01T00:00:00.002', '2016-01-01T00:00:00.003', '2016-01-01T00:00:00.004', '2016-01-01T00:00:00.005', '2016-01-01T00:00:00.006', '2016-01-01T00:00:00.007', '2016-01-01T00:00:00.008', '2016-01-01T00:00:00.009', '2016-01-01T00:00:00.010', '2016-01-01T00:00:00.011', '2016-01-01T00:00:00.012', '2016-01-01T00:00:00.013', '2016-01-01T00:00:00.014', '2016-01-01T00:00:00.015', '2016-01-01T00:00:00.016', '2016-01-01T00:00:00.017', '2016-01-01T00:00:00.018', '2016-01-01T00:00:00.019'], dtype='datetime64[ms]')
简单应用:
import matplotlib.pyplot as plt %matplotlib inline np.random.seed(3000) rnd = np.random.standard_normal(len(dt1)).cumsum() ** 2 fig = plt.figure() plt.plot(dt1.astype(datetime.datetime), rnd) plt.grid(True) fig.autofmt_xdate()
效果图: