Python for Finance: Analyze Big Financial Data

(Elle) #1

NumPy


Since NumPy 1.7, there has been native date-time information support in NumPy. The basic


class is called datetime64:


In  [ 46 ]: import numpy as np
In [ 47 ]: nd = np.datetime64(‘2015-10-31’)
nd
Out[47]: numpy.datetime64(‘2015-10-31’)

Like datetime objects, datetime64 objects can be represented as string objects:


In  [ 48 ]: np.datetime_as_string(nd)
Out[48]: ‘2015-10-31’

Every such object has metadata stored with it, which can be accessed via the


datetime_data method. The two main components are the frequency information (e.g., D


for day) and the unit (e.g., 1 for one day in our case):


In  [ 49 ]: np.datetime_data(nd)
Out[49]: (‘D’, 1)

A datetime64 object can easily be constructed from a datetime object:


In  [ 50 ]: d
Out[50]: datetime.datetime(2016, 10, 31, 10, 5, 30, 500000)
In [ 51 ]: nd = np.datetime64(d)
nd
Out[51]: numpy.datetime64(‘2016-10-31T11:05:30.500000+0100’)

Similarly, using the astype method, a datetime64 object can be converted into a


datetime object:


In  [ 52 ]: nd.astype(dt.datetime)
Out[52]: datetime.datetime(2016, 10, 31, 10, 5, 30, 500000)

Another way to construct such an object is by providing a string object, e.g., with year


and month, and the frequency information. Note that in the following case, the object


value defaults to the first day of the month:


In  [ 53 ]: nd  =   np.datetime64(‘2015-10’,    ‘D’)
nd
Out[53]: numpy.datetime64(‘2015-10-01’)

Comparing two datetime64 objects yields a True value whenever the information given is


the same — even if the level of detail is different:


In  [ 54 ]: np.datetime64(‘2015-10’)    ==  np.datetime64(‘2015-10-01’)
Out[54]: True

Of course, you can also define ndarray objects containing multiple datetime64 objects:


In  [ 55 ]: np.array([‘2016-06-10’, ‘2016-07-10’,   ‘2016-08-10’],  dtype=‘datetime64’)
Out[55]: array([‘2016-06-10’, ‘2016-07-10’, ‘2016-08-10’], dtype=‘datetime64[D]’)
In [ 56 ]: np.array([‘2016-06-10T12:00:00’, ‘2016-07-10T12:00:00’,
‘2016-08-10T12:00:00’], dtype=‘datetime64[s]’)
Out[56]: array([‘2016-06-10T12:00:00+0200’, ‘2016-07-10T12:00:00+0200’,
‘2016-08-10T12:00:00+0200’], dtype=‘datetime64[s]’)

You can also generate ranges of dates by using the function arange. Different frequencies


(e.g., days, months, or weeks) are easily taken care of:


In  [ 57 ]: np.arange(‘2016-01-01’, ‘2016-01-04’,   dtype=‘datetime64’)
# daily frequency as default in this case
Free download pdf