Ranks the data. Equal values are assigned a rank that is the average of the ranks that would have been otherwise assigned to all of the values within that set. Ranks begin at 1, not 0. If pct is True, computes percentage ranks. NaNs in the input array are re
(
self,
dim: Hashable,
*,
pct: bool = False,
keep_attrs: bool | None = None,
)
| 8361 | return new.assign_coords(quantile=q) |
| 8362 | |
| 8363 | def rank( |
| 8364 | self, |
| 8365 | dim: Hashable, |
| 8366 | *, |
| 8367 | pct: bool = False, |
| 8368 | keep_attrs: bool | None = None, |
| 8369 | ) -> Self: |
| 8370 | """Ranks the data. |
| 8371 | |
| 8372 | Equal values are assigned a rank that is the average of the ranks that |
| 8373 | would have been otherwise assigned to all of the values within |
| 8374 | that set. |
| 8375 | Ranks begin at 1, not 0. If pct is True, computes percentage ranks. |
| 8376 | |
| 8377 | NaNs in the input array are returned as NaNs. |
| 8378 | |
| 8379 | The `bottleneck` library is required. |
| 8380 | |
| 8381 | Parameters |
| 8382 | ---------- |
| 8383 | dim : Hashable |
| 8384 | Dimension over which to compute rank. |
| 8385 | pct : bool, default: False |
| 8386 | If True, compute percentage ranks, otherwise compute integer ranks. |
| 8387 | keep_attrs : bool or None, optional |
| 8388 | If True, the dataset's attributes (`attrs`) will be copied from |
| 8389 | the original object to the new one. If False, the new |
| 8390 | object will be returned without attributes. |
| 8391 | |
| 8392 | Returns |
| 8393 | ------- |
| 8394 | ranked : Dataset |
| 8395 | Variables that do not depend on `dim` are dropped. |
| 8396 | """ |
| 8397 | if not OPTIONS["use_bottleneck"]: |
| 8398 | raise RuntimeError( |
| 8399 | "rank requires bottleneck to be enabled." |
| 8400 | " Call `xr.set_options(use_bottleneck=True)` to enable it." |
| 8401 | ) |
| 8402 | |
| 8403 | if dim not in self.dims: |
| 8404 | raise ValueError( |
| 8405 | f"Dimension {dim!r} not found in data dimensions {tuple(self.dims)}" |
| 8406 | ) |
| 8407 | |
| 8408 | variables = {} |
| 8409 | for name, var in self.variables.items(): |
| 8410 | if name in self.data_vars: |
| 8411 | if dim in var.dims: |
| 8412 | variables[name] = var.rank(dim, pct=pct) |
| 8413 | else: |
| 8414 | variables[name] = var |
| 8415 | |
| 8416 | coord_names = set(self.coords) |
| 8417 | if keep_attrs is None: |
| 8418 | keep_attrs = _get_keep_attrs(default=True) |
| 8419 | attrs = self.attrs if keep_attrs else None |
| 8420 | return self._replace(variables, coord_names, attrs=attrs) |