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

Function _adjust_bin_edges

xarray/core/resample_cftime.py:249–293  ·  view source on GitHub ↗

This is required for determining the bin edges resampling with month end, quarter end, and year end frequencies. Consider the following example. Let's say you want to downsample the time series with the following coordinates to month end frequency: CFTimeIndex([2000-01-01 12:00:00

(
    datetime_bins: CFTimeIndex,
    freq: BaseCFTimeOffset,
    closed: SideOptions,
    index: CFTimeIndex,
    labels: CFTimeIndex,
)

Source from the content-addressed store, hash-verified

247
248
249def _adjust_bin_edges(
250 datetime_bins: CFTimeIndex,
251 freq: BaseCFTimeOffset,
252 closed: SideOptions,
253 index: CFTimeIndex,
254 labels: CFTimeIndex,
255) -> tuple[CFTimeIndex, CFTimeIndex]:
256 """This is required for determining the bin edges resampling with
257 month end, quarter end, and year end frequencies.
258
259 Consider the following example. Let's say you want to downsample the
260 time series with the following coordinates to month end frequency:
261
262 CFTimeIndex([2000-01-01 12:00:00, 2000-01-31 12:00:00,
263 2000-02-01 12:00:00], dtype='object')
264
265 Without this adjustment, _get_time_bins with month-end frequency will
266 return the following index for the bin edges (default closed='right' and
267 label='right' in this case):
268
269 CFTimeIndex([1999-12-31 00:00:00, 2000-01-31 00:00:00,
270 2000-02-29 00:00:00], dtype='object')
271
272 If 2000-01-31 is used as a bound for a bin, the value on
273 2000-01-31T12:00:00 (at noon on January 31st), will not be included in the
274 month of January. To account for this, pandas adds a day minus one worth
275 of microseconds to the bin edges generated by cftime range, so that we do
276 bin the value at noon on January 31st in the January bin. This results in
277 an index with bin edges like the following:
278
279 CFTimeIndex([1999-12-31 23:59:59, 2000-01-31 23:59:59,
280 2000-02-29 23:59:59], dtype='object')
281
282 The labels are still:
283
284 CFTimeIndex([2000-01-31 00:00:00, 2000-02-29 00:00:00], dtype='object')
285 """
286 if isinstance(freq, MonthEnd | QuarterEnd | YearEnd):
287 if closed == "right":
288 datetime_bins = datetime_bins + datetime.timedelta(days=1, microseconds=-1)
289 if datetime_bins[-2] > index.max():
290 datetime_bins = datetime_bins[:-1]
291 labels = labels[:-1]
292
293 return datetime_bins, labels
294
295
296def _get_range_edges(

Callers 1

_get_time_binsFunction · 0.85

Calls 1

maxMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…