Appendix A. Dates and Times

As in the majority of scientific disciplines, dates and times play an important role in finance. This appendix introduces different aspects of this topic when it comes to Python programming. It cannot, of course, be exhaustive. However, it provides an introduction to the main areas of the Python ecosystem that support the modeling of date and time information.

Python

The datetime module from the Python standard library allows for the implementation of the most important date and time–related tasks:

In [1]: from pylab import mpl, plt
        plt.style.use('seaborn')
        mpl.rcParams['font.family'] = 'serif'
        %matplotlib inline

In [2]: import datetime as dt

In [3]: dt.datetime.now()  1
Out[3]: datetime.datetime(2018, 10, 19, 15, 17, 32, 164295)

In [4]: to = dt.datetime.today()  1
        to
Out[4]: datetime.datetime(2018, 10, 19, 15, 17, 32, 177092)

In [5]: type(to)
Out[5]: datetime.datetime

In [6]: dt.datetime.today().weekday()  2
Out[6]: 4
1

Returns the exact date and system time.

Returns the day of the week as a number, where 0 = Monday.

Of course, datetime objects can be ...

Get Python for Finance, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.