Path: blob/main/resources/week-3/DateFunctionality_ed.ipynb
3223 views
Kernel: Python 3
In today's lecture, where we'll be looking at the time series and date functionally in pandas. Manipulating dates and time is quite flexible in Pandas and thus allows us to conduct more analysis such as time series analysis, which we will talk about soon. Actually, pandas was originally created by Wed McKinney to handle date and time data when he worked as a consultant for hedge funds.
In [1]:
Timestamp
In [2]:
Out[2]:
Timestamp('2019-09-01 10:05:00')
In [3]:
Out[3]:
Timestamp('2019-12-20 00:00:00')
In [4]:
Out[4]:
5
In [5]:
Out[5]:
23
Period
In [6]:
Out[6]:
Period('2016-01', 'M')
In [7]:
Out[7]:
Period('2016-03-05', 'D')
In [8]:
Out[8]:
Period('2016-06', 'M')
In [9]:
Out[9]:
Period('2016-03-03', 'D')
In [10]:
DatetimeIndex and PeriodIndex
In [11]:
Out[11]:
2016-09-01 a
2016-09-02 b
2016-09-03 c
dtype: object
In [12]:
Out[12]:
pandas.core.indexes.datetimes.DatetimeIndex
In [13]:
Out[13]:
2016-09 d
2016-10 e
2016-11 f
Freq: M, dtype: object
In [14]:
Out[14]:
pandas.core.indexes.period.PeriodIndex
Converting to Datetime
In [15]:
Out[15]:
In [16]:
Out[16]:
In [17]:
Out[17]:
Timestamp('2012-07-04 00:00:00')
Timedelta
In [18]:
Out[18]:
Timedelta('2 days 00:00:00')
In [19]:
Out[19]:
Timestamp('2016-09-14 11:10:00')
Offset
In [20]:
Out[20]:
6
In [21]:
Out[21]:
Timestamp('2016-09-11 00:00:00')
In [22]:
Out[22]:
Timestamp('2016-09-30 00:00:00')
Working with Dates in a Dataframe
In [23]:
Out[23]:
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 [24]:
Out[24]:
DatetimeIndex(['2016-10-03', '2016-10-04', '2016-10-05', '2016-10-06',
'2016-10-07', '2016-10-10', '2016-10-11', '2016-10-12',
'2016-10-13'],
dtype='datetime64[ns]', freq='B')
In [25]:
Out[25]:
DatetimeIndex(['2016-06-01', '2016-09-01', '2016-12-01', '2017-03-01',
'2017-06-01', '2017-09-01', '2017-12-01', '2018-03-01',
'2018-06-01', '2018-09-01', '2018-12-01', '2019-03-01'],
dtype='datetime64[ns]', freq='QS-JUN')
In [26]:
Out[26]:
In [27]:
Out[27]:
Index(['Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday', 'Sunday',
'Sunday', 'Sunday'],
dtype='object')
In [28]:
Out[28]:
In [29]:
Out[29]:
In [30]:
Out[30]:
In [31]:
Out[31]:
In [32]:
Out[32]:
In [33]:
Out[33]: