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

Method _round_via_method

xarray/coding/cftimeindex.py:710–730  ·  view source on GitHub ↗

Round dates using a specified method.

(self, freq, method)

Source from the content-addressed store, hash-verified

708 return infer_freq(self)
709
710 def _round_via_method(self, freq, method):
711 """Round dates using a specified method."""
712 from xarray.coding.cftime_offsets import CFTIME_TICKS, Day, to_offset
713
714 if not self._data.size:
715 return CFTimeIndex(np.array(self))
716
717 offset = to_offset(freq)
718 if isinstance(offset, Day):
719 # Following pandas, "In the 'round' context, Day unambiguously
720 # means 24h, not calendar-day"
721 offset_as_timedelta = timedelta(days=offset.n)
722 elif isinstance(offset, CFTIME_TICKS):
723 offset_as_timedelta = offset.as_timedelta()
724 else:
725 raise ValueError(f"{offset} is a non-fixed frequency")
726
727 unit = _total_microseconds(offset_as_timedelta)
728 values = self.asi8
729 rounded = method(values, unit)
730 return _cftimeindex_from_i8(rounded, self.date_type, self.name)
731
732 def floor(self, freq):
733 """Round dates down to fixed frequency.

Callers 3

floorMethod · 0.95
ceilMethod · 0.95
roundMethod · 0.95

Calls 6

to_offsetFunction · 0.90
CFTimeIndexClass · 0.85
_total_microsecondsFunction · 0.85
methodClass · 0.85
_cftimeindex_from_i8Function · 0.85
as_timedeltaMethod · 0.45

Tested by

no test coverage detected