pandas.tseries.offsets.DateOffset#
- class pandas.tseries.offsets.DateOffset[源代码]#
用于日期范围的标准日期增量类型。
其工作方式与 relativedelta 的关键字参数形式完全相同。请注意,relativedelta 的位置参数形式不受支持。建议不要使用关键字 n - 您最好在使用的关键字中指定 n,但无论如何它在那里。n 对于 DateOffset 子类是必需的。
DateOffset 的工作方式如下。每个偏移量都指定了一组符合 DateOffset 的日期。例如,Bday 将此集定义为工作日(周一至周五)的日期集。要测试一个日期是否属于 DateOffset dateOffset 的集合,我们可以使用 is_on_offset 方法:dateOffset.is_on_offset(date)。
如果一个日期不在有效日期上,可以使用 rollback 和 rollforward 方法将日期滚动到该日期之前/之后的最近有效日期。
DateOffset 可以被创建为向前移动给定数量的有效日期。例如,Bday(2) 可以添加到日期上,将其向前移动两个工作日。如果日期不是从有效日期开始的,它首先会被滚动到一个有效日期。因此,伪代码是:
def __add__(date): date = rollback(date) # does nothing if date is valid return date + <n number of periods>
当一个日期偏移量是为负周期数创建时,日期首先被向前滚动。伪代码是:
def __add__(date): date = rollforward(date) # does nothing if date is valid return date + <n number of periods>
零会带来问题。是应该向前滚动还是向后滚动?我们任意将其向前滚动。
date + BDay(0) == BDay.rollforward(date)
由于 0 有点奇怪,我们建议避免使用它。
此外,添加由日期组件的单数形式指定的 DateOffset 可用于替换时间戳的某些组件。
属性
Return the count of the number of periods.
Return boolean whether the frequency can align with midnight.
weekday
(int {0, 1, ..., 6}, default 0) 一周中的具体整数。 - 0 代表周一 - 1 代表周二 - 2 代表周三 - 3 代表周四 - 4 代表周五 - 5 代表周六 - 6 代表周日 也可以使用 dateutil.relativedelta 中的 Weekday 类型。 - MO 代表周一 - TU 代表周二 - WE 代表周三 - TH 代表周四 - FR 代表周五 - SA 代表周六 - SU 代表周日。
**kwds
在偏移量值上加或替换的临时参数。 **添加** 到偏移量的参数(如 Timedelta): - years - months - weeks - days - hours - minutes - seconds - milliseconds - microseconds - nanoseconds **替换** 偏移量值的参数: - year - month - day - weekday - hour - minute - second - microsecond - nanosecond。
另请参阅
dateutil.relativedelta.relativedeltarelativedelta 类型旨在应用于现有 datetime,并且可以替换该 datetime 的特定组件,或表示时间间隔。
示例
>>> from pandas.tseries.offsets import DateOffset >>> ts = pd.Timestamp('2017-01-01 09:10:11') >>> ts + DateOffset(months=3) Timestamp('2017-04-01 09:10:11')
>>> ts = pd.Timestamp('2017-01-01 09:10:11') >>> ts + DateOffset(months=2) Timestamp('2017-03-01 09:10:11') >>> ts + DateOffset(day=31) Timestamp('2017-01-31 09:10:11')
>>> ts + pd.DateOffset(hour=8) Timestamp('2017-01-01 08:10:11')
属性
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.
Return a string representing the base frequency.
Returns an integer of the total number of nanoseconds for fixed frequencies.
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.