Python for Finance: Analyze Big Financial Data

(Elle) #1
Out[57]:    array([‘2016-01-01’,    ‘2016-01-02’,   ‘2016-01-03’],  dtype=‘datetime64[D]’)
In [ 58 ]: np.arange(‘2016-01-01’, ‘2016-10-01’, dtype=‘datetime64[M]’)
# monthly frequency
Out[58]: array([‘2016-01’, ‘2016-02’, ‘2016-03’, ‘2016-04’, ‘2016-05’, ‘2016-06’,
‘2016-07’, ‘2016-08’, ‘2016-09’], dtype=‘datetime64[M]’)
In [ 59 ]: np.arange(‘2016-01-01’, ‘2016-10-01’, dtype=‘datetime64[W]’)[: 10 ]
# weekly frequency
Out[59]: array([‘2015-12-31’, ‘2016-01-07’, ‘2016-01-14’, ‘2016-01-21’,
‘2016-01-28’, ‘2016-02-04’, ‘2016-02-11’, ‘2016-02-18’,
‘2016-02-25’, ‘2016-03-03’], dtype=‘datetime64[W]’)

You can also easily use subday frequencies, like hours or seconds (refer to the


documentation for all options):


In  [ 60 ]: dtl =   np.arange(‘2016-01-01T00:00:00’,    ‘2016-01-02T00:00:00’,
dtype=‘datetime64[h]’)
# hourly frequency
dtl[: 10 ]
Out[60]: array([‘2016-01-01T00+0100’, ‘2016-01-01T01+0100’, ‘2016-01-01T02+0100’,
‘2016-01-01T03+0100’, ‘2016-01-01T04+0100’, ‘2016-01-01T05+0100’,
‘2016-01-01T06+0100’, ‘2016-01-01T07+0100’, ‘2016-01-01T08+0100’,
‘2016-01-01T09+0100’], dtype=‘datetime64[h]’)

Plotting date-time and/or time series data can sometimes be tricky. matplotlib has good


support for standard datetime objects. Transforming datetime64 information into


datetime information generally does the trick, as the following example, whose result is


shown in Figure C-1, illustrates:


In  [ 61 ]: import matplotlib.pyplot as plt
%matplotlib inline
In [ 62 ]: np.random.seed( 3000 )
rnd = np.random.standard_normal(len(dtl)).cumsum() ** 2
In [ 63 ]: fig = plt.figure()
plt.plot(dtl.astype(dt.datetime), rnd)
# convert np.datetime to datetime.datetime
plt.grid(True)
fig.autofmt_xdate()
# autoformatting of datetime x-ticks

Figure C-1. Plot with datetime.datetime x-ticks autoformatted

Finally, we also have an illustration of using arange with seconds and milliseconds as


frequencies:


In  [ 64 ]: np.arange(‘2016-01-01T00:00:00’,    ‘2016-01-02T00:00:00’,
dtype=‘datetime64[s]’)[: 10 ]
# seconds as frequency
Out[64]: array([‘2016-01-01T00:00:00+0100’, ‘2016-01-01T00:00:01+0100’,
‘2016-01-01T00:00:02+0100’, ‘2016-01-01T00:00:03+0100’,
‘2016-01-01T00:00:04+0100’, ‘2016-01-01T00:00:05+0100’,
‘2016-01-01T00:00:06+0100’, ‘2016-01-01T00:00:07+0100’,
Free download pdf