本文整理汇总了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)))
示例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")
示例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]))