pandas.tseries.offsets.BaseOffset#
- class pandas.tseries.offsets.BaseOffset#
Base class for all pandas date offsets.
Every offset in
pandas.tseries.offsetsis a subclass ofBaseOffset, soisinstance(obj, BaseOffset)is the way to check whether an object is a pandas offset.BaseOffsetis not meant to be instantiated directly; to write your own offset, subclass it as described in the Notes.Attributes
Return the count of the number of periods.
Return boolean whether the frequency can align with midnight.
See also
tseries.offsets.DateOffsetOffset backed by a
dateutil.relativedelta.tseries.frequencies.to_offsetConvert a string or timedelta to an offset.
Notes
A subclass defines:
_add_datetime(self, other), which receives aTimestampand returns theTimestampself.nsteps away. As in the built-in offsets, forn < 0rolling back onto the offset counts as the first step, andn == 0rolls forward. This implements+and-and is used bydate_range().is_on_offset(self, dt), whichrollback,rollforwardanddate_range()use to tell whetherdtalready falls on the offset._prefix, the name shown in the offset’sfreqstr. It is not registered as a frequency string, so pass the offset object itself asfreq.
+and-honornormalize=Trueonly if_add_datetimechecksself.normalize. Adding a custom offset to aSeriesorDatetimeIndexapplies it one element at a time and emits aPerformanceWarning.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
baseReturns a copy of the calling offset object with n=1 and all other attributes equal.
Return a string representing the frequency.
Return a dict of extra parameters for the offset.
Return the count of the number of periods.
(DEPRECATED) Return a string representing the base frequency.
Return an integer of the total number of nanoseconds.
Return boolean whether the frequency can align with midnight.
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.