MCPcopy
hub / github.com/pydata/xarray / _decode_datetime_with_pandas

Function _decode_datetime_with_pandas

xarray/coding/times.py:472–532  ·  view source on GitHub ↗
(
    flat_num_dates: np.ndarray,
    units: str,
    calendar: str,
    time_resolution: PDDatetimeUnitOptions = "ns",
)

Source from the content-addressed store, hash-verified

470
471
472def _decode_datetime_with_pandas(
473 flat_num_dates: np.ndarray,
474 units: str,
475 calendar: str,
476 time_resolution: PDDatetimeUnitOptions = "ns",
477) -> np.ndarray:
478 if not _is_standard_calendar(calendar):
479 raise OutOfBoundsDatetime(
480 f"Cannot decode times from a non-standard calendar, {calendar!r}, using "
481 "pandas."
482 )
483
484 # Work around pandas.to_timedelta issue with dtypes smaller than int64 and
485 # NumPy 2.0 by casting all int and uint data to int64 and uint64,
486 # respectively. See https://github.com/pandas-dev/pandas/issues/56996 for
487 # more details.
488 if flat_num_dates.dtype.kind == "i":
489 flat_num_dates = flat_num_dates.astype(np.int64)
490 elif flat_num_dates.dtype.kind == "u":
491 flat_num_dates = flat_num_dates.astype(np.uint64)
492
493 try:
494 time_unit, ref_date = _unpack_time_unit_and_ref_date(units)
495 ref_date = _align_reference_date_and_unit(ref_date, time_unit)
496 # here the highest wanted resolution is set
497 ref_date = _align_reference_date_and_unit(ref_date, time_resolution)
498 except ValueError as err:
499 # ValueError is raised by pd.Timestamp for non-ISO timestamp
500 # strings, in which case we fall back to using cftime
501 raise OutOfBoundsDatetime from err
502
503 _check_date_is_after_shift(ref_date, calendar)
504
505 with warnings.catch_warnings():
506 warnings.filterwarnings("ignore", "invalid value encountered", RuntimeWarning)
507 if flat_num_dates.size > 0:
508 # avoid size 0 datetimes GH1329
509 _check_date_for_units_since_refdate(
510 flat_num_dates.min(), time_unit, ref_date
511 )
512 _check_date_for_units_since_refdate(
513 flat_num_dates.max(), time_unit, ref_date
514 )
515
516 # To avoid integer overflow when converting to nanosecond units for integer
517 # dtypes smaller than np.int64 cast all integer and unsigned integer dtype
518 # arrays to np.int64 (GH 2002, GH 6589). Note this is safe even in the case
519 # of np.uint64 values, because any np.uint64 value that would lead to
520 # overflow when converting to np.int64 would not be representable with a
521 # timedelta64 value, and therefore would raise an error in the lines above.
522 if flat_num_dates.dtype.kind in "iu":
523 flat_num_dates = flat_num_dates.astype(np.int64)
524 elif flat_num_dates.dtype.kind == "f":
525 flat_num_dates = flat_num_dates.astype(np.float64)
526
527 timedeltas = _numbers_to_timedelta(
528 flat_num_dates, time_unit, ref_date.unit, "datetimes"
529 )

Callers 1

decode_cf_datetimeFunction · 0.85

Calls 9

_is_standard_calendarFunction · 0.85
_numbers_to_timedeltaFunction · 0.85
astypeMethod · 0.45
minMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…