當前位置: 首頁>>代碼示例>>Python>>正文


Python cftime.DatetimeGregorian方法代碼示例

本文整理匯總了Python中cftime.DatetimeGregorian方法的典型用法代碼示例。如果您正苦於以下問題:Python cftime.DatetimeGregorian方法的具體用法?Python cftime.DatetimeGregorian怎麽用?Python cftime.DatetimeGregorian使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cftime的用法示例。


在下文中一共展示了cftime.DatetimeGregorian方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: to_datetime

# 需要導入模塊: import cftime [as 別名]
# 或者: from cftime import DatetimeGregorian [as 別名]
def to_datetime(d):

    if isinstance(d, dt.datetime):
        return d
    if isinstance(d, cftime.DatetimeNoLeap):
        return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
    elif isinstance(d, cftime.DatetimeGregorian):
        return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
    elif isinstance(d, str):
        errors = []
        for fmt in (
                "%Y-%m-%d %H:%M:%S",
                "%Y-%m-%dT%H:%M:%S",
                "%Y-%m-%dT%H:%M:%SZ"):
            try:
                return dt.datetime.strptime(d, fmt)
            except ValueError as e:
                errors.append(e)
                continue
        raise Exception(errors)
    elif isinstance(d, np.datetime64):
        return d.astype(dt.datetime)
    else:
        raise Exception("Unknown value: {} type: {}".format(d, type(d))) 
開發者ID:MetOffice,項目名稱:forest,代碼行數:26,代碼來源:util.py

示例2: ensure_cftime_array

# 需要導入模塊: import cftime [as 別名]
# 或者: from cftime import DatetimeGregorian [as 別名]
def ensure_cftime_array(time: Sequence):
    """Convert an input 1D array to an array of cftime objects. Python's datetime are converted to cftime.DatetimeGregorian.

    Raises ValueError when unable to cast the input.
    """
    if isinstance(time, xr.DataArray):
        time = time.indexes["time"]
    elif isinstance(time, np.ndarray):
        time = pd.DatetimeIndex(time)
    if isinstance(time[0], cftime.datetime):
        return time
    if isinstance(time[0], pydt.datetime):
        return np.array(
            [cftime.DatetimeGregorian(*ele.timetuple()[:6]) for ele in time]
        )
    raise ValueError("Unable to cast array to cftime dtype") 
開發者ID:Ouranosinc,項目名稱:xclim,代碼行數:18,代碼來源:calendar.py

示例3: test_cftime_transform_gregorian_no_warn

# 需要導入模塊: import cftime [as 別名]
# 或者: from cftime import DatetimeGregorian [as 別名]
def test_cftime_transform_gregorian_no_warn(self):
        try:
            import cftime
        except:
            raise SkipTest('Test requires cftime library')
        gregorian_dates = [cftime.DatetimeGregorian(2000, 2, 28),
                           cftime.DatetimeGregorian(2000, 3, 1),
                           cftime.DatetimeGregorian(2000, 3, 2)]
        curve = Curve((gregorian_dates, [1, 2, 3]))
        plot = bokeh_renderer.get_plot(curve)
        xs = plot.handles['cds'].data['x']
        self.assertEqual(xs.astype('int64'),
                         np.array([951696000000, 951868800000, 951955200000])) 
開發者ID:holoviz,項目名稱:holoviews,代碼行數:15,代碼來源:testelementplot.py


注:本文中的cftime.DatetimeGregorian方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。