当前位置: 首页>>代码示例>>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;未经允许,请勿转载。