Python cftime.DatetimeGregorian() Examples
The following are 3
code examples of cftime.DatetimeGregorian().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cftime
, or try the search function
.
Example #1
Source File: util.py From forest with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)))
Example #2
Source File: calendar.py From xclim with Apache License 2.0 | 6 votes |
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")
Example #3
Source File: testelementplot.py From holoviews with BSD 3-Clause "New" or "Revised" License | 5 votes |
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]))