In [21]:
# generating series of time
import pandas as pd
import numpy as np

In [22]:
# 1. specify start date, period and frequency 
t_month = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'M')
t_month
# defaults to end of month


Out[22]:
DatetimeIndex(['2017-07-31 10:15:00', '2017-08-31 10:15:00',
               '2017-09-30 10:15:00', '2017-10-31 10:15:00',
               '2017-11-30 10:15:00', '2017-12-31 10:15:00',
               '2018-01-31 10:15:00', '2018-02-28 10:15:00',
               '2018-03-31 10:15:00', '2018-04-30 10:15:00'],
              dtype='datetime64[ns]', freq='M')

In [23]:
# 1. specify start date 
t_month_start = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'MS')
t_month_start
#default to start of the month
# month is a month, it is either start or end
# preserves time


Out[23]:
DatetimeIndex(['2017-08-01 10:15:00', '2017-09-01 10:15:00',
               '2017-10-01 10:15:00', '2017-11-01 10:15:00',
               '2017-12-01 10:15:00', '2018-01-01 10:15:00',
               '2018-02-01 10:15:00', '2018-03-01 10:15:00',
               '2018-04-01 10:15:00', '2018-05-01 10:15:00'],
              dtype='datetime64[ns]', freq='MS')

In [24]:
#frequency hourly
t_hour = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'H')
t_hour


Out[24]:
DatetimeIndex(['2017-07-15 10:15:00', '2017-07-15 11:15:00',
               '2017-07-15 12:15:00', '2017-07-15 13:15:00',
               '2017-07-15 14:15:00', '2017-07-15 15:15:00',
               '2017-07-15 16:15:00', '2017-07-15 17:15:00',
               '2017-07-15 18:15:00', '2017-07-15 19:15:00'],
              dtype='datetime64[ns]', freq='H')

In [25]:
#days
t_days = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'D')
t_days


Out[25]:
DatetimeIndex(['2017-07-15 10:15:00', '2017-07-16 10:15:00',
               '2017-07-17 10:15:00', '2017-07-18 10:15:00',
               '2017-07-19 10:15:00', '2017-07-20 10:15:00',
               '2017-07-21 10:15:00', '2017-07-22 10:15:00',
               '2017-07-23 10:15:00', '2017-07-24 10:15:00'],
              dtype='datetime64[ns]', freq='D')

In [26]:
#business days
t_business_days = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'B')
t_business_days


Out[26]:
DatetimeIndex(['2017-07-17 10:15:00', '2017-07-18 10:15:00',
               '2017-07-19 10:15:00', '2017-07-20 10:15:00',
               '2017-07-21 10:15:00', '2017-07-24 10:15:00',
               '2017-07-25 10:15:00', '2017-07-26 10:15:00',
               '2017-07-27 10:15:00', '2017-07-28 10:15:00'],
              dtype='datetime64[ns]', freq='B')

In [34]:
# start, end , freq
x = pd.date_range(start = '2017 Jul 15 10:15', 
              end = '2017 Jul 25', 
              freq='8H')

In [35]:
len(x)


Out[35]:
29

In [37]:
# add time zone
tzome = pd.date_range(start = '2017 Jul 15 10:15', 
              end = '2017 Jul 25', 
              freq='8H', tz = 'Asia/Hong_Kong' )
tzome


Out[37]:
DatetimeIndex(['2017-07-15 10:15:00+08:00', '2017-07-15 18:15:00+08:00',
               '2017-07-16 02:15:00+08:00', '2017-07-16 10:15:00+08:00',
               '2017-07-16 18:15:00+08:00', '2017-07-17 02:15:00+08:00',
               '2017-07-17 10:15:00+08:00', '2017-07-17 18:15:00+08:00',
               '2017-07-18 02:15:00+08:00', '2017-07-18 10:15:00+08:00',
               '2017-07-18 18:15:00+08:00', '2017-07-19 02:15:00+08:00',
               '2017-07-19 10:15:00+08:00', '2017-07-19 18:15:00+08:00',
               '2017-07-20 02:15:00+08:00', '2017-07-20 10:15:00+08:00',
               '2017-07-20 18:15:00+08:00', '2017-07-21 02:15:00+08:00',
               '2017-07-21 10:15:00+08:00', '2017-07-21 18:15:00+08:00',
               '2017-07-22 02:15:00+08:00', '2017-07-22 10:15:00+08:00',
               '2017-07-22 18:15:00+08:00', '2017-07-23 02:15:00+08:00',
               '2017-07-23 10:15:00+08:00', '2017-07-23 18:15:00+08:00',
               '2017-07-24 02:15:00+08:00', '2017-07-24 10:15:00+08:00',
               '2017-07-24 18:15:00+08:00'],
              dtype='datetime64[ns, Asia/Hong_Kong]', freq='8H')

In [38]:
#normalize --> start and end date set to midnight
t_norm = pd.date_range(start = '2017 Jul 15 10:15', 
              end = '2017 Jul 25', 
              freq='8H',
              tz = 'Asia/Hong_Kong', normalize = True )
t_norm


Out[38]:
DatetimeIndex(['2017-07-15 00:00:00+08:00', '2017-07-15 08:00:00+08:00',
               '2017-07-15 16:00:00+08:00', '2017-07-16 00:00:00+08:00',
               '2017-07-16 08:00:00+08:00', '2017-07-16 16:00:00+08:00',
               '2017-07-17 00:00:00+08:00', '2017-07-17 08:00:00+08:00',
               '2017-07-17 16:00:00+08:00', '2017-07-18 00:00:00+08:00',
               '2017-07-18 08:00:00+08:00', '2017-07-18 16:00:00+08:00',
               '2017-07-19 00:00:00+08:00', '2017-07-19 08:00:00+08:00',
               '2017-07-19 16:00:00+08:00', '2017-07-20 00:00:00+08:00',
               '2017-07-20 08:00:00+08:00', '2017-07-20 16:00:00+08:00',
               '2017-07-21 00:00:00+08:00', '2017-07-21 08:00:00+08:00',
               '2017-07-21 16:00:00+08:00', '2017-07-22 00:00:00+08:00',
               '2017-07-22 08:00:00+08:00', '2017-07-22 16:00:00+08:00',
               '2017-07-23 00:00:00+08:00', '2017-07-23 08:00:00+08:00',
               '2017-07-23 16:00:00+08:00', '2017-07-24 00:00:00+08:00',
               '2017-07-24 08:00:00+08:00', '2017-07-24 16:00:00+08:00',
               '2017-07-25 00:00:00+08:00'],
              dtype='datetime64[ns, Asia/Hong_Kong]', freq='8H')

In [39]:
type(t_norm[0])


Out[39]:
pandas._libs.tslib.Timestamp

In [41]:
# timestamp
pd.Timestamp('2018-07-1')


Out[41]:
Timestamp('2018-07-01 00:00:00')

In [46]:
t_stamp = pd.Timestamp('2017-10-23 19:00:23.0123456789')
t_stamp


Out[46]:
Timestamp('2017-10-23 19:00:23.012345678')

In [47]:
t_stamp.year


Out[47]:
2017

In [48]:
t_stamp.month


Out[48]:
10

In [49]:
t_stamp.weekday_name


Out[49]:
'Monday'

In [56]:
# time offset
td = pd.Timedelta('2H 1 day 1us')
td


Out[56]:
Timedelta('1 days 02:00:00.000001')

In [57]:
t_stamp + td


Out[57]:
Timestamp('2017-10-24 21:00:23.012346678')

In [58]:
# time span --> Period has start and end time 
p  = pd.Period('7/2016')
p


Out[58]:
Period('2016-07', 'M')

In [59]:
t = pd.Timestamp('7/21/2016')
t


Out[59]:
Timestamp('2016-07-21 00:00:00')

In [67]:
p.start_time < t < p.end_time


Out[67]:
True

In [68]:
# range of periods
p_range = pd.period_range('2016-01-01 12:15', freq = '60T', periods=10)
p_range


Out[68]:
PeriodIndex(['2016-01-01 12:15', '2016-01-01 13:15', '2016-01-01 14:15',
             '2016-01-01 15:15', '2016-01-01 16:15', '2016-01-01 17:15',
             '2016-01-01 18:15', '2016-01-01 19:15', '2016-01-01 20:15',
             '2016-01-01 21:15'],
            dtype='period[60T]', freq='60T')

In [72]:
# indexing time series with date_range
num_periods = 40
ts_dt = pd.Series(range(num_periods), 
                  pd.date_range('2016-07-01 11:15', 
                                freq = '60T', 
                                periods = num_periods ))
ts_dt.head()


Out[72]:
2016-07-01 11:15:00    0
2016-07-01 12:15:00    1
2016-07-01 13:15:00    2
2016-07-01 14:15:00    3
2016-07-01 15:15:00    4
Freq: 60T, dtype: int64

In [74]:
type(ts_dt.index)


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

In [75]:
type(ts_dt.index[0])


Out[75]:
pandas._libs.tslib.Timestamp

In [76]:
ts_dt['2016-7-1 11']


Out[76]:
2016-07-01 11:15:00    0
Freq: 60T, dtype: int64

In [77]:
num_periods = 40
ts_dt_rng = pd.Series(range(num_periods), 
                  pd.period_range('2016-07-01 11:15', 
                                freq = '60T', 
                                periods = num_periods ))
ts_dt_rng.head()


Out[77]:
2016-07-01 11:15    0
2016-07-01 12:15    1
2016-07-01 13:15    2
2016-07-01 14:15    3
2016-07-01 15:15    4
Freq: 60T, dtype: int64

In [79]:
type(ts_dt_rng.index)


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

In [80]:
type(ts_dt_rng.index[0])


Out[80]:
pandas._libs.period.Period

In [87]:
ts_pd = ts_dt.to_period(freq='60T')
ts_pd.tail()


Out[87]:
2016-07-02 22:15    35
2016-07-02 23:15    36
2016-07-03 00:15    37
2016-07-03 01:15    38
2016-07-03 02:15    39
Freq: 60T, dtype: int64

In [90]:
type(ts_pd.index[0])


Out[90]:
pandas._libs.period.Period

In [94]:
per = pd.to_datetime(ts_dt_rng)
per.head()


Out[94]:
2016-07-01 11:15   1970-01-01 00:00:00.000000000
2016-07-01 12:15   1970-01-01 00:00:00.000000001
2016-07-01 13:15   1970-01-01 00:00:00.000000002
2016-07-01 14:15   1970-01-01 00:00:00.000000003
2016-07-01 15:15   1970-01-01 00:00:00.000000004
Freq: 60T, dtype: datetime64[ns]

grab everything from july first and second


In [96]:
ts_dt['2016-07-01']


Out[96]:
2016-07-01 11:15:00     0
2016-07-01 12:15:00     1
2016-07-01 13:15:00     2
2016-07-01 14:15:00     3
2016-07-01 15:15:00     4
2016-07-01 16:15:00     5
2016-07-01 17:15:00     6
2016-07-01 18:15:00     7
2016-07-01 19:15:00     8
2016-07-01 20:15:00     9
2016-07-01 21:15:00    10
2016-07-01 22:15:00    11
2016-07-01 23:15:00    12
Freq: 60T, dtype: int64

In [101]:
#partial string index inclusive
ts_dt['2016-07-01':'2016-07-02']


Out[101]:
2016-07-01 11:15:00     0
2016-07-01 12:15:00     1
2016-07-01 13:15:00     2
2016-07-01 14:15:00     3
2016-07-01 15:15:00     4
2016-07-01 16:15:00     5
2016-07-01 17:15:00     6
2016-07-01 18:15:00     7
2016-07-01 19:15:00     8
2016-07-01 20:15:00     9
2016-07-01 21:15:00    10
2016-07-01 22:15:00    11
2016-07-01 23:15:00    12
2016-07-02 00:15:00    13
2016-07-02 01:15:00    14
2016-07-02 02:15:00    15
2016-07-02 03:15:00    16
2016-07-02 04:15:00    17
2016-07-02 05:15:00    18
2016-07-02 06:15:00    19
2016-07-02 07:15:00    20
2016-07-02 08:15:00    21
2016-07-02 09:15:00    22
2016-07-02 10:15:00    23
2016-07-02 11:15:00    24
2016-07-02 12:15:00    25
2016-07-02 13:15:00    26
2016-07-02 14:15:00    27
2016-07-02 15:15:00    28
2016-07-02 16:15:00    29
2016-07-02 17:15:00    30
2016-07-02 18:15:00    31
2016-07-02 19:15:00    32
2016-07-02 20:15:00    33
2016-07-02 21:15:00    34
2016-07-02 22:15:00    35
2016-07-02 23:15:00    36
Freq: 60T, dtype: int64

In [102]:
ts_dt['july 2 2016': ]


Out[102]:
2016-07-02 00:15:00    13
2016-07-02 01:15:00    14
2016-07-02 02:15:00    15
2016-07-02 03:15:00    16
2016-07-02 04:15:00    17
2016-07-02 05:15:00    18
2016-07-02 06:15:00    19
2016-07-02 07:15:00    20
2016-07-02 08:15:00    21
2016-07-02 09:15:00    22
2016-07-02 10:15:00    23
2016-07-02 11:15:00    24
2016-07-02 12:15:00    25
2016-07-02 13:15:00    26
2016-07-02 14:15:00    27
2016-07-02 15:15:00    28
2016-07-02 16:15:00    29
2016-07-02 17:15:00    30
2016-07-02 18:15:00    31
2016-07-02 19:15:00    32
2016-07-02 20:15:00    33
2016-07-02 21:15:00    34
2016-07-02 22:15:00    35
2016-07-02 23:15:00    36
2016-07-03 00:15:00    37
2016-07-03 01:15:00    38
2016-07-03 02:15:00    39
Freq: 60T, dtype: int64

create pd.Timestamp with European stype date string


In [110]:
pd.to_datetime('15/1/2016', dayfirst=True)


Out[110]:
Timestamp('2016-01-15 00:00:00')

In [111]:
date = pd.to_datetime('15/1/2016')
date.year


Out[111]:
2016

In [112]:
date.month


Out[112]:
1

In [113]:
date.day


Out[113]:
15

In [115]:
date.strftime(format = '%Y')


Out[115]:
'2016'

In [122]:
date.strftime(format = '%y-%m-%d')


Out[122]:
'16-01-15'

indexing


In [130]:
ts_pd['jul 2, 2016': : 3]


Out[130]:
2016-07-02 00:15    13
2016-07-02 03:15    16
2016-07-02 06:15    19
2016-07-02 09:15    22
2016-07-02 12:15    25
2016-07-02 15:15    28
2016-07-02 18:15    31
2016-07-02 21:15    34
2016-07-03 00:15    37
Freq: 60T, dtype: int64

In [132]:
ts_pd['jul 2, 2016': : -2]


Out[132]:
2016-07-02 23:15    36
2016-07-02 21:15    34
2016-07-02 19:15    32
2016-07-02 17:15    30
2016-07-02 15:15    28
2016-07-02 13:15    26
2016-07-02 11:15    24
2016-07-02 09:15    22
2016-07-02 07:15    20
2016-07-02 05:15    18
2016-07-02 03:15    16
2016-07-02 01:15    14
2016-07-01 23:15    12
2016-07-01 21:15    10
2016-07-01 19:15     8
2016-07-01 17:15     6
2016-07-01 15:15     4
2016-07-01 13:15     2
2016-07-01 11:15     0
Freq: 60T, dtype: int64

In [ ]: