DateOffset and TimeDelta objects

A DateOffset object represents a change or offset in time. The key features of a DateOffset object are as follows:

  • This can be added to/subtracted from a datetime object to obtain a shifted date.
  • This can be multiplied by an integer (positive or negative) so that the increment can be applied multiple times.
  • This has the rollforward and rollback methods to move a date forward to the next offset date or backward to the previous offset date.

Let's create some date objects using the datetime method in pandas:

    In [371]: xmasDay=pd.datetime(2014,12,25)
              xmasDay
    Out[371]: datetime.datetime(2014, 12, 25, 0, 0)
    
    In [373]: boxingDay=xmasDay+pd.DateOffset(days=1)
              boxingDay
    Out[373]: Timestamp('2014-12-26 00:00:00', tz=None)
    
    In [390}: today=pd.datetime.now()
              today
    Out[390]: datetime.datetime(2014, 5, 31, 13, 7, 36, 440060)  

Note that datetime.datetime is different from pd.Timestamp. The former is a Python class and is inefficient, while the latter is based on the numpy.datetime64 datatype.

The pd.DateOffset object works with pd.Timestamp, and adding it to a datetime.datetime function casts that object into a pd.Timestamp object.

The following illustrates the command for 1 week from today:

    In [392]: today+pd.DateOffset(weeks=1)
    Out[392]: Timestamp('2014-06-07 13:07:36.440060', tz=None)  

 

The following illustrates the command for 5 years from today:

    In [394]: today+2*pd.DateOffset(years=2, months=6)
    Out[394]: Timestamp('2019-05-30 13:07:36.440060', tz=None) 

Here is an example of using the rollforward functionality. QuarterBegin is a DateOffset object that is used to increment a given datetime object to the start of the next calendar quarter:

    In [18]: lastDay=pd.datetime(2013,12,31)
    In [24]: from pandas.tseries.offsets import QuarterBegin
            dtoffset=QuarterBegin()
            lastDay+dtoffset
    Out[24]: Timestamp('2014-03-01 00:00:00', tz=None)
    
    In [25]: dtoffset.rollforward(lastDay)
    Out[25]: Timestamp('2014-03-01 00:00:00', tz=None)
  

Thus, we can see that the next quarter after December 31, 2013 starts on March 1, 2014. Timedelta is similar to DateOffset but works with datetime.datetime objects. The use of these objects is explained with the following command:

    In [40]: weekDelta=datetime.timedelta(weeks=1)
             weekDelta
    Out[40]: datetime.timedelta(7)
    
    In [39]: today=pd.datetime.now()
             today
    Out[39]: datetime.datetime (2014, 6, 2, 3, 56, 0, 600309)
    
    In [41]: today+weekDelta
    Out[41]: datetime.datetime (2014, 6, 9, 3, 56,0, 600309)  

We have learned about datatypes, conversions between datatypes, date offsets, separating time components from timestamps, and so on, up to now. Next, we will see how we can apply some mathematical operators such as lagging, shifting, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.156.113