DateTime Index

Often the time and date information won’t just be in a separate column, but instead be the actual datatime index, so let's learn how to deal with this sort of data with pandas!

Imports

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Built-in Date and Time Library

Note that from datetime import datetime is differnt from import datetime

In [2]:
from datetime import datetime

Create a DateTime Object

To illustrate the order of arguments Note that hour in 24 hour format

In [3]:
# To illustrate the order of arguments
my_year = 2017
my_month = 1
my_day = 2
my_hour = 13
my_minute = 30
my_second = 15

Create a datetime object -Use datetime() method to create a datetime object, January 2nd, 2017

In [4]:
my_date = datetime(my_year,my_month,my_day)

Time Defaults to 0:00

In [5]:
my_date 
Out[5]:
datetime.datetime(2017, 1, 2, 0, 0)

You can specify hours, minutes, seconds in hh, mm, ss format January 2nd, 2017 at 13:30:15

In [6]:
my_date_time = datetime(my_year,my_month,my_day,my_hour,my_minute,my_second)

Alternatively,

In [7]:
my_date_time
Out[7]:
datetime.datetime(2017, 1, 2, 13, 30, 15)

The data type is datetime.datetime

In [8]:
type(my_date_time)
Out[8]:
datetime.datetime

Get DateTime Info

You can grab any part of the datetime object you want

In [8]:
my_date.day
Out[8]:
2
In [9]:
my_date_time.hour
Out[9]:
13

Using Pandas with Datetime Index

You'll usually deal with time series as an index when working with pandas dataframes obtained from some sort of financial API. Fortunately pandas has a lot of functions and methods to work with time series!

Create an Example Datetime List/Array

Use datetime() method to create a datetime object

In [13]:
first_two = [datetime(2016, 1, 1), datetime(2016, 1, 2)]
first_two
Out[13]:
[datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 1, 2, 0, 0)]

Converted to an DateTime Index .DatetimeIndex()

To create an index, pass the list created to DatetimeIndex()

In [14]:
dt_ind = pd.DatetimeIndex(first_two)
dt_ind
Out[14]:
DatetimeIndex(['2016-01-01', '2016-01-02'], dtype='datetime64[ns]', freq=None)

Create a Pandas DataFrame .DateFrame()

Attached Some Random Data

In [29]:
data = np.random.randn(2,2)
print(data)
cols = ['A','B']
[[-1.58270607  0.47766839]
 [ 0.34171008  0.5889566 ]]

Use DataFrame() to create a pandas DataFrame

In [30]:
df = pd.DataFrame(data,dt_ind,cols)
df
Out[30]:
A B
2016-01-01 0.165224 -0.767629
2016-01-02 -0.482305 0.307934
In [31]:
df.index
Out[31]:
DatetimeIndex(['2016-01-01', '2016-01-02'], dtype='datetime64[ns]', freq=None)

Get Latest Date Location .argmax()

In [34]:
df.index.argmax()
Out[34]:
1

Get Lastest Location Data .max()

In [35]:
df.index.max()
Out[35]:
Timestamp('2016-01-02 00:00:00')

Get Earliest Date Index Location .argmin()

In [37]:
df.index.argmin()
Out[37]:
0

Get Earliest Location Data .min()

In [38]:
df.index.min()
Out[38]:
Timestamp('2016-01-01 00:00:00')