当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python matplotlib relativedelta用法及代码示例


本文简要介绍 python 语言中 matplotlib.dates.relativedelta 的用法。

用法

class matplotlib.dates.relativedelta(dt1=None, dt2=None, years=0, months=0, days=0, leapdays=0, weeks=0, hours=0, minutes=0, seconds=0, microseconds=0, year=None, month=None, day=None, weekday=None, yearday=None, nlyearday=None, hour=None, minute=None, second=None, microsecond=None)

基础: object

relativedelta 类型旨在应用于现有日期时间,并且可以替换该日期时间的特定组件,或表示时间间隔。

它基于 M.-A 所做的出色工作的规范。 Lemburg 在他的mx.DateTime 扩展中。但是,请注意,这种类型NOT 实现了与他的工作相同的算法。 NOT 是否期望它的行为类似于 mx.DateTime 的对应物。

有两种不同的方式来构建 relativedelta 实例。第一个是传递两个日期/日期时间类:

relativedelta(datetime1, datetime2)

第二个是传递任意数量的以下关键字参数:

relativedelta(arg1=x,arg2=y,arg3=z...)

year, month, day, hour, minute, second, microsecond:
    Absolute information (argument is singular); adding or subtracting a
    relativedelta with absolute information does not perform an arithmetic
    operation, but rather REPLACES the corresponding value in the
    original datetime with the value(s) in relativedelta.

years, months, weeks, days, hours, minutes, seconds, microseconds:
    Relative information, may be negative (argument is plural); adding
    or subtracting a relativedelta with relative information performs
    the corresponding arithmetic operation on the original datetime value
    with the information in the relativedelta.

weekday: 
    One of the weekday instances (MO, TU, etc) available in the
    relativedelta module. These instances may receive a parameter N,
    specifying the Nth weekday, which could be positive or negative
    (like MO(+1) or MO(-2)). Not specifying it is the same as specifying
    +1. You can also use an integer, where 0=MO. This argument is always
    relative e.g. if the calculated date is already Monday, using MO(1)
    or MO(-1) won't change the day. To effectively make it absolute, use
    it in combination with the day argument (e.g. day=1, MO(1) for first
    Monday of the month).

leapdays:
    Will add given days to the date found, if year is a leap
    year, and the date found is post 28 of february.

yearday, nlyearday:
    Set the yearday or the non-leap year day (jump leap days).
    These are converted to day/month/leapdays information.

关键字参数有相对形式和绝对形式。复数是相对的,单数是绝对的。对于以下顺序中的每个参数,首先应用绝对形式(通过将每个属性设置为该值),然后应用相对形式(通过将值添加到属性)。

将此 relativedelta 添加到日期时间时考虑的属性顺序为:

  1. Year

  2. Month

  3. Day

  4. Hours

  5. Minutes

  6. Seconds

  7. Microseconds

最后,使用上述规则应用工作日。

例如

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta, MO
>>> dt = datetime(2018, 4, 9, 13, 37, 0)
>>> delta = relativedelta(hours=25, day=1, weekday=MO(1))
>>> dt + delta
datetime.datetime(2018, 4, 2, 14, 37)

首先将天设置为 1(每月的第一天),然后添加 25 小时,以获得第 2 天和第 14 小时,最后应用工作日,但由于第 2 天已经是星期一,因此没有效果.

相关用法


注:本文由纯净天空筛选整理自skytowner.com大神的英文原创作品 matplotlib.dates.relativedelta。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。