Interpolate values according to different methods.
(
self,
dim: Hashable | None = None,
use_coordinate: bool | str = True,
method: InterpOptions = "linear",
limit: int | None = None,
max_gap: (
int | float | str | pd.Timedelta | np.timedelta64 | dt.timedelta | None
) = None,
keep_attrs: bool | None = None,
**kwargs,
)
| 329 | |
| 330 | |
| 331 | def interp_na( |
| 332 | self, |
| 333 | dim: Hashable | None = None, |
| 334 | use_coordinate: bool | str = True, |
| 335 | method: InterpOptions = "linear", |
| 336 | limit: int | None = None, |
| 337 | max_gap: ( |
| 338 | int | float | str | pd.Timedelta | np.timedelta64 | dt.timedelta | None |
| 339 | ) = None, |
| 340 | keep_attrs: bool | None = None, |
| 341 | **kwargs, |
| 342 | ): |
| 343 | """Interpolate values according to different methods.""" |
| 344 | from xarray.coding.cftimeindex import CFTimeIndex |
| 345 | |
| 346 | if dim is None: |
| 347 | raise NotImplementedError("dim is a required argument") |
| 348 | |
| 349 | if limit is not None: |
| 350 | valids = _get_valid_fill_mask(self, dim, limit) |
| 351 | |
| 352 | if max_gap is not None: |
| 353 | max_type = type(max_gap).__name__ |
| 354 | if not is_scalar(max_gap): |
| 355 | raise ValueError("max_gap must be a scalar.") |
| 356 | |
| 357 | if ( |
| 358 | dim in self._indexes |
| 359 | and isinstance( |
| 360 | self._indexes[dim].to_pandas_index(), pd.DatetimeIndex | CFTimeIndex |
| 361 | ) |
| 362 | and use_coordinate |
| 363 | ): |
| 364 | # Convert to float |
| 365 | max_gap = timedelta_to_numeric(max_gap) |
| 366 | |
| 367 | if not use_coordinate and not isinstance(max_gap, Number | np.number): |
| 368 | raise TypeError( |
| 369 | f"Expected integer or floating point max_gap since use_coordinate=False. Received {max_type}." |
| 370 | ) |
| 371 | |
| 372 | # method |
| 373 | index = get_clean_interp_index(self, dim, use_coordinate=use_coordinate) |
| 374 | interp_class, kwargs = _get_interpolator(method, **kwargs) |
| 375 | interpolator = partial(func_interpolate_na, interp_class, **kwargs) |
| 376 | |
| 377 | if keep_attrs is None: |
| 378 | keep_attrs = _get_keep_attrs(default=True) |
| 379 | |
| 380 | with warnings.catch_warnings(): |
| 381 | warnings.filterwarnings("ignore", "overflow", RuntimeWarning) |
| 382 | warnings.filterwarnings("ignore", "invalid value", RuntimeWarning) |
| 383 | arr = apply_ufunc( |
| 384 | interpolator, |
| 385 | self, |
| 386 | index, |
| 387 | input_core_dims=[[dim], [dim]], |
| 388 | output_core_dims=[[dim]], |
no test coverage detected
searching dependent graphs…