Representing data intervals with date offsets

DatetimeIndex objects are created at various frequencies by using passing frequency strings such as 'M', 'W', and 'BM' using the freq parameter of pd.date_range(). Under the hood, these frequency strings are translated into an instance of the pandas DateOffset object.

A DateOffset represents a regular frequency increment. Specific date offset logic, such as "month", "business day", or "hour", is represented in pandas with various subclasses of DateOffset. A DateOffset provides pandas with the intelligence to be able to determine how to calculate a specific interval of time from a reference date and time. This provides the user of pandas much greater flexibility in representing date/time offsets than just using a fixed numeric interval

A useful and practical example is calculating the next day of business. This is not simply determined by adding one day to a datetime. If the date represents a Friday, the next business day in the US financial market is not Saturday but Monday. And in some cases, one business day from a Friday may actually be Tuesday if Monday is a holiday. pandas gives us all the tools required to handle these types of tricky scenarios.

Let's examine this in action by generating a date range using 'B' as the frequency:

This time-series has omitted 2014-08-30 and 2014-08-30 as they are Saturday and Sunday and not considered a business day.

A DatetimeIndex has a .freq property that represents the frequency of the timestamps in the index:

Notice that pandas has created an instance of the BusinessDay class to represent the DateOffset unit of this index. As mentioned earlier, pandas represents different date offsets with a subclass of the DateOffset class. The following are the various built-in date offset classes that are provided by pandas:

Class

Description

DateOffset

Generic offset defaults to one calendar day

BDay

Business day

CDay

Custom business day

Week

One week, optionally anchored on a day of the week

WeekOfMonth

The x-th day of the y-th week of each month

LastWeekOfMonth

The x-th day of the last week of each month

MonthEnd

Calendar month end

MonthBegin

Calendar month start

BMonthEnd

Business month end

BMonthBegin

Business month start

CBMonthEnd

Custom business month end

CBMonthBegin

Custom business month start

QuarterEnd

Quarter end

QuarterBegin

Quarter start

BQuarterEnd

Business quarter end

BQuarterBegin

Business quarter start

FYS253Quarter

Retail (52-53 week) quarter

YearEnd

Calendar year end

YearBegin

Calendar year start

BYearEnd

Business quarter end

BYearBegin

Business quarter start

FYS253

Retail (52-53 week) year

Hour

One hour

Minute

One minute

Second

One second

Milli

One millisecond

Micro

One microsecond

 

pandas uses this strategy of using DateOffset and its specializations to codify logic to calculate the next day. This makes using these objects very flexible as well as powerful. DateOffset objects can be used in various scenarios:

  • They can be added or subtracted to obtain a shifted date
  • They can be multiplied by an integer (positive or negative) so that the increment will be applied multiple times
  • They have rollforward and rollback methods to move a date forward or backward to the next or previous "offset date"

DateOffset objects can be created by passing them a datetime object that represents a fixed duration of time or using a number of keyword arguments. Keyword arguments fall into two general categories. The first category is keywords that represent absolute dates: year, month, day, hour, minute, second, and microsecond. The second category represents relative durations and can be negative values: years, months, weeks, day, hours, minutes, seconds, and microseconds.

The following creates a 1-day offset and adds it to datetime:

The following calculates the next business day from a given date:

Multiple units of a specific DateOffset can be represented via multiplication:

The following demonstrates using a BMonthEnd object to calculate the last business day of a month from a given date (in this case, 2014-09-02):

The following uses the BMonthEnd objects .rollforward() method to calculate the next month end:

Several of the offset classes can be parameterized to provide finer control of the offset behavior. As an example, the following calculates the date of the Tuesday (weekday = 1) in the week prior to 2014-08-31:

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

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