DateOffsets
In the Temporal types – Timedelta recipe back in Chapter 3, Data Types, we introduced the pd.Timedelta type and mentioned how it could be used to shift datetimes by a finite duration, like 10 seconds or 5 days. However, a pd.Timedelta cannot be used to offset a date or datetime by say one month because a month does not always represent the same duration of time. In the Gregorian calendar, months can range in duration from 28–31 days. The month of February is usually 28 days but extends to 29 days for every year that is divisible by 4, unless the year is divisible by 100 but not by 400.
Thinking about these issues all of the time would be rather tedious. Fortunately, pandas takes care of all of the mundane details and just lets you shift dates according to a calendar through the use of the pd.DateOffset object, which we will explore in this recipe.
How to do it
To build a foundational knowledge of how this works, let’s start with a very...