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

Method _partial_date_slice

xarray/coding/cftimeindex.py:312–374  ·  view source on GitHub ↗

Adapted from pandas.tseries.index.DatetimeIndex._partial_date_slice Note that when using a CFTimeIndex, if a partial-date selection returns a single element, it will never be converted to a scalar coordinate; this is in slight contrast to the behavior when using

(self, resolution, parsed)

Source from the content-addressed store, hash-verified

310 return full_repr_str
311
312 def _partial_date_slice(self, resolution, parsed):
313 """Adapted from
314 pandas.tseries.index.DatetimeIndex._partial_date_slice
315
316 Note that when using a CFTimeIndex, if a partial-date selection
317 returns a single element, it will never be converted to a scalar
318 coordinate; this is in slight contrast to the behavior when using
319 a DatetimeIndex, which sometimes will return a DataArray with a scalar
320 coordinate depending on the resolution of the datetimes used in
321 defining the index. For example:
322
323 >>> from cftime import DatetimeNoLeap
324 >>> da = xr.DataArray(
325 ... [1, 2],
326 ... coords=[[DatetimeNoLeap(2001, 1, 1), DatetimeNoLeap(2001, 2, 1)]],
327 ... dims=["time"],
328 ... )
329 >>> da.sel(time="2001-01-01")
330 <xarray.DataArray (time: 1)> Size: 8B
331 array([1])
332 Coordinates:
333 * time (time) object 8B 2001-01-01 00:00:00
334 >>> da = xr.DataArray(
335 ... [1, 2],
336 ... coords=[[pd.Timestamp(2001, 1, 1), pd.Timestamp(2001, 2, 1)]],
337 ... dims=["time"],
338 ... )
339 >>> da.sel(time="2001-01-01")
340 <xarray.DataArray ()> Size: 8B
341 array(1)
342 Coordinates:
343 time datetime64[us] 8B 2001-01-01
344 >>> da = xr.DataArray(
345 ... [1, 2],
346 ... coords=[[pd.Timestamp(2001, 1, 1, 1), pd.Timestamp(2001, 2, 1)]],
347 ... dims=["time"],
348 ... )
349 >>> da.sel(time="2001-01-01")
350 <xarray.DataArray (time: 1)> Size: 8B
351 array([1])
352 Coordinates:
353 * time (time) datetime64[us] 8B 2001-01-01T01:00:00
354 """
355 start, end = _parsed_string_to_bounds(self.date_type, resolution, parsed)
356
357 times = self._data
358
359 if self.is_monotonic_increasing:
360 if len(times) and (
361 (start < times[0] and end < times[0])
362 or (start > times[-1] and end > times[-1])
363 ):
364 # we are out of range
365 raise KeyError
366
367 # a monotonic (sorted) series can be sliced
368 left = times.searchsorted(start, side="left")
369 right = times.searchsorted(end, side="right")

Callers 1

_get_string_sliceMethod · 0.95

Calls 1

_parsed_string_to_boundsFunction · 0.85

Tested by

no test coverage detected