Python: Numpy library based on the analysis - Detailed datetime type of processing

Python: Numpy library based on the analysis - Detailed datetime type of processing

Treatment with respect to time, Python comes with a module processing time there is time, datetime, calendar, in addition to the expansion of third-party libraries, such as dateutil and so on. Python to be free to use the processing time through these channels. When we did the analysis with NumPy library, how to convert time?

Beginning in NumPy 1.7 version of its core array (ndarray) datetime object supports these functions, because the 'datetime' This data type name is already used in Python datetime module comes in, the type of data NumPy time called 'datetime64' .

Format string into a single time numpy datetime objects may be used datetime64 instantiate an object, as follows:

#时间字符串转numpy.datetime64
datetime_nd=np.datetime64('2019-01-01')
print(type(datetime_nd))#<class 'numpy.datetime64'>
复制代码

Datetime objects numpy in turn converted to the time format string, use datetime_as_string () function, as follows:

#numpy.datetime64转时间字符串
datetime_str=np.datetime_as_string(datetime_nd)
print(type(datetime_str))#<class 'numpy.str_'>
复制代码

When datetime objects to create a numpy array (Array) format string from an array of time, it can be used directly numpy.array () function, as designated dtype 'datetime64', so the array elements 'datetime64' type, as shown below :

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'>
复制代码

The default time may be a function, a range starting at a given time to create an array of objects numpy datetime (array) by numpy.arange (), as designated dtype 'datetime64' on a daily time interval, as follows:

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']
复制代码

Setting numpy.arange () dtype function parameters can be adjusted interval, such as years, months, weeks, or even an array generation time interval hours, minutes, and degrees of milliseconds, and Python this module is the same datetime divided into units of the date and time units. As follows:

# 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 converted into the format datetime datetime format, using asType () method converting data types, as follows:

#numpy.datetime64转化为datetime格式
datetime_df=datetime_nd.astype(datetime.datetime)
print(type(datetime_df))#<class 'datetime.date'>
复制代码

Further, also it provides the ability numpy datetime.timedelta class and supports two operation time of the object to obtain a value of a time unit forms. Because the core of numpy arrays (ndarray) object has no physical system (physical quantities system), so creating a timedelta64 data types to supplement datetime64. datetime timedelta binding and provides a simpler method of calculation datetime. As follows:

# 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
复制代码

What we do not know the place, you can leave a message Oh!


Reproduced in: https: //juejin.im/post/5d01f0e46fb9a07edd2a0c53

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/93183674