當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。