MCPcopy Index your code
hub / github.com/pydata/xarray / shift

Method shift

xarray/coding/cftimeindex.py:459–515  ·  view source on GitHub ↗

Shift the CFTimeIndex a multiple of the given frequency. See the documentation for :py:func:`~xarray.date_range` for a complete listing of valid frequency strings. Parameters ---------- periods : int, float if freq of days or below Periods to shi

(  # type: ignore[override,unused-ignore]
        self,
        periods: int | float,
        freq: str | timedelta | BaseCFTimeOffset | None = None,
    )

Source from the content-addressed store, hash-verified

457 return self.__contains__(key)
458
459 def shift( # type: ignore[override,unused-ignore]
460 self,
461 periods: int | float,
462 freq: str | timedelta | BaseCFTimeOffset | None = None,
463 ) -> Self:
464 """Shift the CFTimeIndex a multiple of the given frequency.
465
466 See the documentation for :py:func:`~xarray.date_range` for a
467 complete listing of valid frequency strings.
468
469 Parameters
470 ----------
471 periods : int, float if freq of days or below
472 Periods to shift by
473 freq : str, datetime.timedelta or BaseCFTimeOffset
474 A frequency string or datetime.timedelta object to shift by
475
476 Returns
477 -------
478 CFTimeIndex
479
480 See Also
481 --------
482 pandas.DatetimeIndex.shift
483
484 Examples
485 --------
486 >>> index = xr.date_range("2000", periods=1, freq="ME", use_cftime=True)
487 >>> index
488 CFTimeIndex([2000-01-31 00:00:00],
489 dtype='object', length=1, calendar='standard', freq=None)
490 >>> index.shift(1, "ME")
491 CFTimeIndex([2000-02-29 00:00:00],
492 dtype='object', length=1, calendar='standard', freq=None)
493 >>> index.shift(1.5, "24h")
494 CFTimeIndex([2000-02-01 12:00:00],
495 dtype='object', length=1, calendar='standard', freq=None)
496 """
497 from xarray.coding.cftime_offsets import BaseCFTimeOffset
498
499 if freq is None:
500 # None type is required to be compatible with base pd.Index class
501 raise TypeError(
502 f"`freq` argument cannot be None for {type(self).__name__}.shift"
503 )
504
505 if isinstance(freq, timedelta):
506 return self + periods * freq
507
508 if isinstance(freq, str | BaseCFTimeOffset):
509 from xarray.coding.cftime_offsets import to_offset
510
511 return self + periods * to_offset(freq)
512
513 raise TypeError(
514 f"'freq' must be of type str or datetime.timedelta, got {type(freq)}."
515 )
516

Callers

nothing calls this directly

Calls 2

to_offsetFunction · 0.90
typeFunction · 0.85

Tested by

no test coverage detected