pandas
The pandas library was specifically designed with time series data in mind. Therefore, the
library provides classes that are able to efficiently handle date-time information, like the
DatetimeIndex class for time indices (cf. the documentation):
In [ 66 ]: import pandas as pd
Date-time information in pandas is generally stored as a Timestamp object:
In [ 67 ]: ts = pd.Timestamp(‘2016-06-30’)
ts
Out[67]: Timestamp(‘2016-06-30 00:00:00’)
Such objects are easily transformed into regular datetime objects with the to_datetime
method:
In [ 68 ]: d = ts.to_datetime()
d
Out[68]: datetime.datetime(2016, 6, 30, 0, 0)
Similarly, a Timestamp object is straightforwardly constructed from a datetime object:
In [ 69 ]: pd.Timestamp(d)
Out[69]: Timestamp(‘2016-06-30 00:00:00’)
or from a NumPy datetime64 object:
In [ 70 ]: pd.Timestamp(nd)
Out[70]: Timestamp(‘2015-10-01 00:00:00’)
Another important class is the DatetimeIndex class, which is a collection of Timestamp
objects with a number of powerful methods attached (cf. http://bit.ly/date_range_doc and
function, which is rather flexible and powerful for constructing time indices (see
Chapter 6 for more details on this function):
In [ 71 ]: dti = pd.date_range(‘2016/01/01’, freq=‘M’, periods= 12 )
dti
Out[71]: <class ‘pandas.tseries.index.DatetimeIndex’>
[2016-01-31, ..., 2016-12-31]
Length: 12, Freq: M, Timezone: None
Single elements of the object are accessed by the usual indexing operations:
In [ 72 ]: dti[ 6 ]
Out[72]: Timestamp(‘2016-07-31 00:00:00’, offset=‘M’)
DatetimeIndex objects can be transformed into arrays of datetime objects through the
method to_pydatetime:
In [ 73 ]: pdi = dti.to_pydatetime()
pdi
Out[73]: array([datetime.datetime(2016, 1, 31, 0, 0),
datetime.datetime(2016, 2, 29, 0, 0),
datetime.datetime(2016, 3, 31, 0, 0),
datetime.datetime(2016, 4, 30, 0, 0),
datetime.datetime(2016, 5, 31, 0, 0),
datetime.datetime(2016, 6, 30, 0, 0),
datetime.datetime(2016, 7, 31, 0, 0),
datetime.datetime(2016, 8, 31, 0, 0),
datetime.datetime(2016, 9, 30, 0, 0),
datetime.datetime(2016, 10, 31, 0, 0),
datetime.datetime(2016, 11, 30, 0, 0),
datetime.datetime(2016, 12, 31, 0, 0)], dtype=object)