pandas.tseries.offsets.BaseOffset#

class pandas.tseries.offsets.BaseOffset#

Base class for all pandas date offsets.

Every offset in pandas.tseries.offsets is a subclass of BaseOffset, so isinstance(obj, BaseOffset) is the way to check whether an object is a pandas offset. BaseOffset is not meant to be instantiated directly; to write your own offset, subclass it as described in the Notes.

Attributes

n

Return the count of the number of periods.

normalize

Return boolean whether the frequency can align with midnight.

See also

tseries.offsets.DateOffset

Offset backed by a dateutil.relativedelta.

tseries.frequencies.to_offset

Convert a string or timedelta to an offset.

Notes

A subclass defines:

  • _add_datetime(self, other), which receives a Timestamp and returns the Timestamp self.n steps away. As in the built-in offsets, for n < 0 rolling back onto the offset counts as the first step, and n == 0 rolls forward. This implements + and - and is used by date_range().

  • is_on_offset(self, dt), which rollback, rollforward and date_range() use to tell whether dt already falls on the offset.

  • _prefix, the name shown in the offset’s freqstr. It is not registered as a frequency string, so pass the offset object itself as freq.

+ and - honor normalize=True only if _add_datetime checks self.normalize. Adding a custom offset to a Series or DatetimeIndex applies it one element at a time and emits a PerformanceWarning.

Examples

>>> pd.offsets.Hour(5).n
5
>>> pd.offsets.Hour(5).normalize
False
>>> isinstance(pd.offsets.BDay(), pd.offsets.BaseOffset)
True

An offset stepping through dekads, the periods starting on the 1st, 11th and 21st of each month (it ignores normalize):

>>> class Dekad(pd.offsets.BaseOffset):
...     _prefix = "DEKAD"
...     def _add_datetime(self, other):
...         slot = min((other.day - 1) // 10, 2)
...         n = self.n
...         if n <= 0 and other.day != 1 + 10 * slot:
...             n += 1
...         months, slot = divmod(slot + n, 3)
...         result = other.replace(day=1) + pd.DateOffset(months=months)
...         return result.replace(day=1 + 10 * slot)
...     def is_on_offset(self, dt):
...         return dt.day in (1, 11, 21)
>>> ts = pd.Timestamp("2024-01-25")
>>> ts + Dekad()
Timestamp('2024-02-01 00:00:00')
>>> ts - Dekad()
Timestamp('2024-01-21 00:00:00')
>>> Dekad().rollback(ts)
Timestamp('2024-01-21 00:00:00')
>>> pd.date_range("2024-01-05", periods=4, freq=Dekad())
DatetimeIndex(['2024-01-11', '2024-01-21', '2024-02-01', '2024-02-11'],
              dtype='datetime64[us]', freq='DEKAD')

Attributes

base

Returns a copy of the calling offset object with n=1 and all other attributes equal.

freqstr

Return a string representing the frequency.

kwds

Return a dict of extra parameters for the offset.

n

Return the count of the number of periods.

name

(DEPRECATED) Return a string representing the base frequency.

nanos

Return an integer of the total number of nanoseconds.

normalize

Return boolean whether the frequency can align with midnight.

rule_code

Return a string representing the base frequency.

Methods

copy()

Return a copy of the frequency.

is_month_end(ts)

Return boolean whether a timestamp occurs on the month end.

is_month_start(ts)

Return boolean whether a timestamp occurs on the month start.

is_on_offset(dt)

Return boolean whether a timestamp intersects with this frequency.

is_quarter_end(ts)

Return boolean whether a timestamp occurs on the quarter end.

is_quarter_start(ts)

Return boolean whether a timestamp occurs on the quarter start.

is_year_end(ts)

Return boolean whether a timestamp occurs on the year end.

is_year_start(ts)

Return boolean whether a timestamp occurs on the year start.

rollback(dt)

Roll provided date backward to next offset only if not on offset.

rollforward(dt)

Roll provided date forward to next offset only if not on offset.