Date Functionality in Pandas


In [1]:
import pandas as pd
import numpy as np

Timestamp


In [2]:
pd.Timestamp('9/1/2016 10:05AM')


Out[2]:
Timestamp('2016-09-01 10:05:00')

Period


In [3]:
period=pd.Period('1/2016')
period


Out[3]:
Period('2016-01', 'M')

In [4]:
pd.Period('3/5/2016')


Out[4]:
Period('2016-03-05', 'D')

DatetimeIndex


In [5]:
t1 = pd.Series(list('abc'), [pd.Timestamp('2016-09-01'), pd.Timestamp('2016-09-02'), pd.Timestamp('2016-09-03')])
t1


Out[5]:
2016-09-01    a
2016-09-02    b
2016-09-03    c
dtype: object

In [6]:
type(t1.index)


Out[6]:
pandas.core.indexes.datetimes.DatetimeIndex

PeriodIndex


In [7]:
t2 = pd.Series(list('def'), [pd.Period('2016-09'), pd.Period('2016-10'), pd.Period('2016-11')])
t2


Out[7]:
2016-09    d
2016-10    e
2016-11    f
Freq: M, dtype: object

In [8]:
type(t2.index)


Out[8]:
pandas.core.indexes.period.PeriodIndex

Converting to Datetime


In [9]:
d1 = ['2 June 2013', 'Aug 29, 2014', '2015-06-26', '7/12/16']
ts3 = pd.DataFrame(np.random.randint(10, 100, (4,2)), index=d1, columns=list('ab'))
ts3


Out[9]:
a b
2 June 2013 18 77
Aug 29, 2014 66 77
2015-06-26 67 54
7/12/16 19 36

In [10]:
type(ts3.index)


Out[10]:
pandas.core.indexes.base.Index

In [11]:
ts3.index = pd.to_datetime(ts3.index)
ts3


Out[11]:
a b
2013-06-02 18 77
2014-08-29 66 77
2015-06-26 67 54
2016-07-12 19 36

In [12]:
type(ts3.index)


Out[12]:
pandas.core.indexes.datetimes.DatetimeIndex

In [13]:
pd.to_datetime('4.7.12', dayfirst=True)


Out[13]:
Timestamp('2012-07-04 00:00:00')

Timedeltas


In [14]:
pd.Timestamp('9/3/2016')-pd.Timestamp('9/1/2016')


Out[14]:
Timedelta('2 days 00:00:00')

In [15]:
pd.Timestamp('9/2/2016 8:10AM') + pd.Timedelta('12D 3H')


Out[15]:
Timestamp('2016-09-14 11:10:00')

Working with Dates in a Dataframe


In [16]:
dates = pd.date_range('10-01-2016', periods=9, freq='2W-SUN')
dates


Out[16]:
DatetimeIndex(['2016-10-02', '2016-10-16', '2016-10-30', '2016-11-13',
               '2016-11-27', '2016-12-11', '2016-12-25', '2017-01-08',
               '2017-01-22'],
              dtype='datetime64[ns]', freq='2W-SUN')

In [17]:
df = pd.DataFrame({'Count 1': 100 + np.random.randint(-5, 10, 9).cumsum(),
                  'Count 2': 120 + np.random.randint(-5, 10, 9)}, index=dates)
df


Out[17]:
Count 1 Count 2
2016-10-02 107 120
2016-10-16 102 115
2016-10-30 106 126
2016-11-13 110 126
2016-11-27 112 124
2016-12-11 107 129
2016-12-25 106 125
2017-01-08 110 121
2017-01-22 119 126

In [18]:
df.index.weekday_name


Out[18]:
Index(['Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday',
       'Sunday', 'Sunday'],
      dtype='object')

In [19]:
df.diff()


Out[19]:
Count 1 Count 2
2016-10-02 NaN NaN
2016-10-16 -5.0 -5.0
2016-10-30 4.0 11.0
2016-11-13 4.0 0.0
2016-11-27 2.0 -2.0
2016-12-11 -5.0 5.0
2016-12-25 -1.0 -4.0
2017-01-08 4.0 -4.0
2017-01-22 9.0 5.0

In [20]:
df.resample('M').mean()


Out[20]:
Count 1 Count 2
2016-10-31 105.0 120.333333
2016-11-30 111.0 125.000000
2016-12-31 106.5 127.000000
2017-01-31 114.5 123.500000

In [21]:
df['2017']


Out[21]:
Count 1 Count 2
2017-01-08 110 121
2017-01-22 119 126

In [22]:
df['2016-12']


Out[22]:
Count 1 Count 2
2016-12-11 107 129
2016-12-25 106 125

In [23]:
df['2016-12':]


Out[23]:
Count 1 Count 2
2016-12-11 107 129
2016-12-25 106 125
2017-01-08 110 121
2017-01-22 119 126

In [24]:
df.asfreq('W', method='ffill')


Out[24]:
Count 1 Count 2
2016-10-02 107 120
2016-10-09 107 120
2016-10-16 102 115
2016-10-23 102 115
2016-10-30 106 126
2016-11-06 106 126
2016-11-13 110 126
2016-11-20 110 126
2016-11-27 112 124
2016-12-04 112 124
2016-12-11 107 129
2016-12-18 107 129
2016-12-25 106 125
2017-01-01 106 125
2017-01-08 110 121
2017-01-15 110 121
2017-01-22 119 126

In [25]:
import matplotlib.pyplot as plt
%matplotlib inline

df.plot();