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]:
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]:
In [24]:
#frequency hourly
t_hour = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'H')
t_hour
Out[24]:
In [25]:
#days
t_days = pd.date_range(start='2017 Jul 15 10:15', periods = 10, freq = 'D')
t_days
Out[25]:
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]:
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]:
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]:
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]:
In [39]:
type(t_norm[0])
Out[39]:
In [41]:
# timestamp
pd.Timestamp('2018-07-1')
Out[41]:
In [46]:
t_stamp = pd.Timestamp('2017-10-23 19:00:23.0123456789')
t_stamp
Out[46]:
In [47]:
t_stamp.year
Out[47]:
In [48]:
t_stamp.month
Out[48]:
In [49]:
t_stamp.weekday_name
Out[49]:
In [56]:
# time offset
td = pd.Timedelta('2H 1 day 1us')
td
Out[56]:
In [57]:
t_stamp + td
Out[57]:
In [58]:
# time span --> Period has start and end time
p = pd.Period('7/2016')
p
Out[58]:
In [59]:
t = pd.Timestamp('7/21/2016')
t
Out[59]:
In [67]:
p.start_time < t < p.end_time
Out[67]:
In [68]:
# range of periods
p_range = pd.period_range('2016-01-01 12:15', freq = '60T', periods=10)
p_range
Out[68]:
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]:
In [74]:
type(ts_dt.index)
Out[74]:
In [75]:
type(ts_dt.index[0])
Out[75]:
In [76]:
ts_dt['2016-7-1 11']
Out[76]:
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]:
In [79]:
type(ts_dt_rng.index)
Out[79]:
In [80]:
type(ts_dt_rng.index[0])
Out[80]:
In [87]:
ts_pd = ts_dt.to_period(freq='60T')
ts_pd.tail()
Out[87]:
In [90]:
type(ts_pd.index[0])
Out[90]:
In [94]:
per = pd.to_datetime(ts_dt_rng)
per.head()
Out[94]:
In [96]:
ts_dt['2016-07-01']
Out[96]:
In [101]:
#partial string index inclusive
ts_dt['2016-07-01':'2016-07-02']
Out[101]:
In [102]:
ts_dt['july 2 2016': ]
Out[102]:
In [110]:
pd.to_datetime('15/1/2016', dayfirst=True)
Out[110]:
In [111]:
date = pd.to_datetime('15/1/2016')
date.year
Out[111]:
In [112]:
date.month
Out[112]:
In [113]:
date.day
Out[113]:
In [115]:
date.strftime(format = '%Y')
Out[115]:
In [122]:
date.strftime(format = '%y-%m-%d')
Out[122]:
In [130]:
ts_pd['jul 2, 2016': : 3]
Out[130]:
In [132]:
ts_pd['jul 2, 2016': : -2]
Out[132]:
In [ ]: