Return index to use for x values in interpolation or curve fitting. Parameters ---------- arr : DataArray Array to interpolate or fit to a curve. dim : str Name of dimension along which to fit. use_coordinate : str or bool If use_coordinate is True, the c
(
arr, dim: Hashable, use_coordinate: Hashable | bool = True, strict: bool = True
)
| 244 | |
| 245 | |
| 246 | def get_clean_interp_index( |
| 247 | arr, dim: Hashable, use_coordinate: Hashable | bool = True, strict: bool = True |
| 248 | ): |
| 249 | """Return index to use for x values in interpolation or curve fitting. |
| 250 | |
| 251 | Parameters |
| 252 | ---------- |
| 253 | arr : DataArray |
| 254 | Array to interpolate or fit to a curve. |
| 255 | dim : str |
| 256 | Name of dimension along which to fit. |
| 257 | use_coordinate : str or bool |
| 258 | If use_coordinate is True, the coordinate that shares the name of the |
| 259 | dimension along which interpolation is being performed will be used as the |
| 260 | x values. If False, the x values are set as an equally spaced sequence. |
| 261 | strict : bool |
| 262 | Whether to raise errors if the index is either non-unique or non-monotonic (default). |
| 263 | |
| 264 | Returns |
| 265 | ------- |
| 266 | Variable |
| 267 | Numerical values for the x-coordinates. |
| 268 | |
| 269 | Notes |
| 270 | ----- |
| 271 | If indexing is along the time dimension, datetime coordinates are converted |
| 272 | to time deltas with respect to 1970-01-01. |
| 273 | """ |
| 274 | |
| 275 | # Question: If use_coordinate is a string, what role does `dim` play? |
| 276 | from xarray.coding.cftimeindex import CFTimeIndex |
| 277 | |
| 278 | if use_coordinate is False: |
| 279 | axis = arr.get_axis_num(dim) |
| 280 | return np.arange(arr.shape[axis], dtype=np.float64) |
| 281 | |
| 282 | if use_coordinate is True: |
| 283 | index = arr.get_index(dim) |
| 284 | |
| 285 | else: # string |
| 286 | index = arr.coords[use_coordinate] |
| 287 | if index.ndim != 1: |
| 288 | raise ValueError( |
| 289 | f"Coordinates used for interpolation must be 1D, " |
| 290 | f"{use_coordinate} is {index.ndim}D." |
| 291 | ) |
| 292 | index = index.to_index() |
| 293 | |
| 294 | # TODO: index.name is None for multiindexes |
| 295 | # set name for nice error messages below |
| 296 | if isinstance(index, pd.MultiIndex): |
| 297 | index.name = dim |
| 298 | |
| 299 | if strict: |
| 300 | if not index.is_monotonic_increasing: |
| 301 | raise ValueError(f"Index {index.name!r} must be monotonically increasing") |
| 302 | |
| 303 | if not index.is_unique: |
searching dependent graphs…