Pandas' DateOffset object is a tool for performing time offsets when working with dates and times.
The following are some key features and uses of DateOffset:
Basic concepts
A DateOffset object is similar to Timedelta, but it follows calendar date and time rules rather than performing purely arithmetic calculations of time. This means DateOffset takes into account actual calendar days; for example, when dealing with dates that cross months or years, it correctly calculates the day difference, whereas Timedelta simply adds the specified number of days without considering changes in months and years.
Usage
DateOffset can perform date offset operations via arithmetic operators (such as +) or the apply method.
For example, you can use DateOffset like this to add months or hours:
Example
ts
=
pd.
Timestamp
(
'2017-01-01 09:10:11'
)
ts + DateOffset
(
months
=
3
)
Add three months
ts + DateOffset
(
hours
=
2
)
Add two hours
If no parameters are specified, DateOffset() adds one calendar day by default:
ts + DateOffset() # 默认增加一天
Supported parameters
DateOffset supports a variety of parameters that can specify time units to add or replace, such as years, months, weeks, days, hours, minutes, seconds, and so on. These parameters can be ones that add to the offset value (such as years, months, weeks, days, hours, minutes, seconds, etc.) or ones that replace the offset value (such as year, month, day, weekday, hour, minute, second, etc.).
Special method
DateOffset also provides rollforward() and rollback() methods for rolling a date forward or backward to the nearest valid date. For example, if you use a business day offset (BDay), it skips weekends and rolls directly to the next business day.
Example
friday
=
pd.
Timestamp
(
'2022-01-05'
)
two_business_days
=
2
- pd.
offsets
.
BDay
(
)
friday + two_business_days
Add two business days, skipping weekends
Frequency strings
DateOffset supports frequency strings or offset aliases, which can be passed as the freq parameter. These frequency strings represent DateOffset objects and their subclasses, which define the frequency of a datetime index.
DateOffset is a very useful tool in Pandas for working with time series data; it makes date and time offset operations more consistent with actual calendar rules.
Common functions
DateOffset constructor
| Classes/methods | Description |
|---|---|
pd.DateOffset(**kwargs) |
Create a DateOffset object, supporting custom time offsets. |
Common DateOffset subclasses
| class | Description |
|---|---|
pd.offsets.Day() |
Represents an offset of days. |
pd.offsets.BDay() |
Represents an offset of business days (excluding weekends). |
pd.offsets.Hour() |
Represents an offset of hours. |
pd.offsets.Minute() |
Represents an offset of minutes. |
pd.offsets.Second() |
Represents an offset of seconds. |
pd.offsets.Milli() |
Represents an offset of milliseconds. |
pd.offsets.Micro() |
Represents an offset of microseconds. |
pd.offsets.MonthEnd() |
Represents an offset to the end of the month. |
pd.offsets.MonthBegin() |
Represents an offset to the beginning of the month. |
pd.offsets.YearEnd() |
Represents an offset to the end of the year. |
pd.offsets.YearBegin() |
Represents an offset to the beginning of the year. |
pd.offsets.QuarterEnd() |
Represents an offset to the end of the quarter. |
pd.offsets.QuarterBegin() |
Represents an offset to the beginning of the quarter. |
pd.offsets.Week() |
Represents an offset of weeks. |
pd.offsets.WeekOfMonth() |
Represents an offset to a specific week within a month. |
DateOffset attributes
| property | Description |
|---|---|
DateOffset.name |
Return the name of the DateOffset. |
DateOffset.n |
Return or set the number of offsets. |
DateOffset.normalize |
Return or set whether to normalize the time to midnight. |
DateOffset methods
| Method | Description |
|---|---|
DateOffset.apply(other) |
Apply the offset to another datetime object. |
DateOffset.rollforward(other) |
Roll a date forward to the next offset. |
DateOffset.rollback(other) |
Roll a date backward to the previous offset. |
DateOffset.is_anchored() |
Check whether the offset is anchored (i.e., has a fixed frequency). |
DateOffset.onOffset(date) |
Check whether a date is aligned with the offset. |
Example
Example
import
pandas
as
pd
from
pandas.
tseries
.
offsets
import
Day
,
BDay
,
MonthEnd
Create a DateOffset
offset
=
Day
(
3
)
(
offset
)
Output <3 * Days>
Apply a DateOffset
date
=
pd.
Timestamp
(
'2023-01-01'
)
new_date
=
date + offset
(
new_date
)
Output: 2023-01-04
Using common subclasses
bday_offset
=
BDay
(
2
)
new_bdate
=
date + bday_offset
(
new_bdate
)
Output: 2023-01-05 (skipping weekends)
End-of-month offset
month_end_offset
=
MonthEnd
(
)
new_month_end
=
date + month_end_offset
(
new_month_end
)
Output: 2023-01-31
Detailed parameter description
pd.DateOffset()
| Parameters | Description |
|---|---|
years |
Offset of years. |
months |
Offset of months. |
weeks |
Offset of weeks. |
days |
Offset of days. |
hours |
Offset of hours. |
minutes |
Offset of minutes. |
seconds |
Offset of seconds. |
milliseconds |
Offset of milliseconds. |
microseconds |
Offset of microseconds. |
nanoseconds |
Offset of nanoseconds. |
pd.offsets.BDay()
| Parameters | Description |
|---|---|
n |
The number of offsets, defaulting to 1. |
normalize |
Whether to normalize the time to midnight, defaulting to False。 |
pd.offsets.MonthEnd()
| Parameters | Description |
|---|---|
n |
The number of offsets, defaulting to 1. |
normalize |
Whether to normalize the time to midnight, defaulting to False。 |
For more detailed information, refer to Pandas official documentation。