關(guān)于時(shí)間的處理,,Python中自帶的處理時(shí)間的模塊就有time ,、datetime、calendar,,另外還有擴(kuò)展的第三方庫(kù),,如dateutil等等。通過(guò)這些途徑可以隨心所欲地用Python去處理時(shí)間,。當(dāng)我們用NumPy庫(kù)做數(shù)據(jù)分析時(shí),,如何轉(zhuǎn)換時(shí)間呢? 在NumPy 1.7版本開(kāi)始,它的核心數(shù)組(ndarray)對(duì)象支持datetime相關(guān)功能,,由于’datetime’這個(gè)數(shù)據(jù)類型名稱已經(jīng)在Python自帶的datetime模塊中使用了,, NumPy中時(shí)間數(shù)據(jù)的類型稱為’datetime64’。 單個(gè)時(shí)間格式字符串轉(zhuǎn)換為numpy的datetime對(duì)象,,可使用datetime64實(shí)例化一個(gè)對(duì)象,,如下所示: #時(shí)間字符串轉(zhuǎn)numpy.datetime64 datetime_nd=np.datetime64('2019-01-01') print(type(datetime_nd))#<class 'numpy.datetime64'>
反過(guò)來(lái)numpy的datetime對(duì)象轉(zhuǎn)換為時(shí)間格式字符串,可使用datetime_as_string()函數(shù),,如下所示: #numpy.datetime64轉(zhuǎn)時(shí)間字符串 datetime_str=np.datetime_as_string(datetime_nd) print(type(datetime_str))#<class 'numpy.str_'>
從時(shí)間格式字符串?dāng)?shù)組去創(chuàng)建numpy的datetime對(duì)象數(shù)組(array)時(shí),,可以直接使用numpy.array()函數(shù),,指定dtype為’datetime64’,,這樣的話數(shù)組中的元素為’datetime64’類型,如下所示: datetime_array = np.array(['2019-01-05','2019-01-02','2019-01-03'], dtype='datetime64') print(datetime_array)#['2019-01-05' '2019-01-02' '2019-01-03'] print(type(datetime_array))#<class 'numpy.ndarray'> print(type(datetime_array[0]))#<class 'numpy.datetime64'>
也可以通過(guò)numpy.arange()函數(shù),,給定時(shí)間起始范圍去創(chuàng)建numpy的datetime對(duì)象數(shù)組(array),,指定dtype為’datetime64’時(shí)默認(rèn)以日為時(shí)間間隔,如下所示: datetime_array = np.arange('2019-01-05','2019-01-10', dtype='datetime64') print(datetime_array)#['2019-01-05' '2019-01-06' '2019-01-07' '2019-01-08' '2019-01-09']
設(shè)定numpy.arange()函數(shù)中的dtype參數(shù),,可以調(diào)整時(shí)間的間隔,,比如以年、月,、周,,甚至小時(shí)、分鐘,、毫秒程度的間隔生成時(shí)間數(shù)組,,這點(diǎn)和Python的datetime模塊是一樣的,分為了date單位和time單位,。如下所示: # generate year datetime array datetime_array = np.arange('2018-01-01','2020-01-01', dtype='datetime64[Y]') print(datetime_array)#['2018' '2019'] # generate month datetime array datetime_array = np.arange('2019-01-01','2019-10-01', dtype='datetime64[M]') print(datetime_array)#['2019-01' '2019-02' '2019-03' '2019-04' '2019-05' '2019-06' '2019-07' '2019-08' '2019-09'] # generate week datetime array datetime_array = np.arange('2019-01-05','2019-02-10', dtype='datetime64[W]') print(datetime_array)#['2019-01-03' '2019-01-10' '2019-01-17' '2019-01-24' '2019-01-31'] # generate ms datetime array datetime_array = np.arange('2019-01-05','2019-01-10', dtype='datetime64[ms]') print(datetime_array) #['2019-01-05T00:00:00.000' '2019-01-05T00:00:00.001' # '2019-01-05T00:00:00.002' ... '2019-01-09T23:59:59.997' # '2019-01-09T23:59:59.998' '2019-01-09T23:59:59.999']
將numpy.datetime64轉(zhuǎn)化為datetime格式轉(zhuǎn)換為datetime格式,,可使用astype()方法轉(zhuǎn)換數(shù)據(jù)類型,如下所示: #numpy.datetime64轉(zhuǎn)化為datetime格式 datetime_df=datetime_nd.astype(datetime.datetime) print(type(datetime_df))#<class 'datetime.date'>
另外,,numpy也提供了datetime.timedelta類的功能,,支持兩個(gè)時(shí)間對(duì)象的運(yùn)算,得到一個(gè)時(shí)間單位形式的數(shù)值,。因?yàn)閚umpy的核心數(shù)組(ndarray)對(duì)象沒(méi)有物理量系統(tǒng)(physical quantities system),,所以創(chuàng)建了timedelta64數(shù)據(jù)類型來(lái)補(bǔ)充datetime64。datetime和timedelta結(jié)合提供了更簡(jiǎn)單的datetime計(jì)算方法,。如下所示: # numpy.datetime64 calculations datetime_delta = np.datetime64('2009-01-01') - np.datetime64('2008-01-01') print(datetime_delta)#366 days print(type(datetime_delta))#<class 'numpy.timedelta64'> datetime_delta = np.datetime64('2009') + np.timedelta64(20, 'D') print(datetime_delta)#2009-01-21 datetime_delta = np.datetime64('2011-06-15T00:00') + np.timedelta64(12, 'h') print(datetime_delta)#2011-06-15T12:00 datetime_delta = np.timedelta64(1,'W') / np.timedelta64(1,'D') print(datetime_delta)#7.0
|