-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement idxmax and idxmin functions #3871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,10 @@ | |
|
||
import numpy as np | ||
|
||
from . import duck_array_ops, utils | ||
from . import dtypes, duck_array_ops, utils | ||
from .alignment import deep_align | ||
from .merge import merge_coordinates_without_align | ||
from .nanops import dask_array | ||
from .options import OPTIONS | ||
from .pycompat import dask_array_type | ||
from .utils import is_dict_like | ||
|
@@ -1338,3 +1339,66 @@ def polyval(coord, coeffs, degree_dim="degree"): | |
coords={coord.name: coord, degree_dim: np.arange(deg_coord.max() + 1)[::-1]}, | ||
) | ||
return (lhs * coeffs).sum(degree_dim) | ||
|
||
|
||
def _calc_idxminmax( | ||
*, | ||
array, | ||
func: Callable, | ||
dim: Hashable = None, | ||
skipna: bool = None, | ||
fill_value: Any = dtypes.NA, | ||
keep_attrs: bool = None, | ||
): | ||
"""Apply common operations for idxmin and idxmax.""" | ||
# This function doesn't make sense for scalars so don't try | ||
if not array.ndim: | ||
raise ValueError("This function does not apply for scalars") | ||
|
||
if dim is not None: | ||
pass # Use the dim if available | ||
elif array.ndim == 1: | ||
# it is okay to guess the dim if there is only 1 | ||
dim = array.dims[0] | ||
else: | ||
# The dim is not specified and ambiguous. Don't guess. | ||
raise ValueError("Must supply 'dim' argument for multidimensional arrays") | ||
|
||
if dim not in array.dims: | ||
raise KeyError(f'Dimension "{dim}" not in dimension') | ||
if dim not in array.coords: | ||
raise KeyError(f'Dimension "{dim}" does not have coordinates') | ||
|
||
# These are dtypes with NaN values argmin and argmax can handle | ||
na_dtypes = "cfO" | ||
|
||
if skipna or (skipna is None and array.dtype.kind in na_dtypes): | ||
toddrjen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Need to skip NaN values since argmin and argmax can't handle them | ||
allna = array.isnull().all(dim) | ||
array = array.where(~allna, 0) | ||
|
||
# This will run argmin or argmax. | ||
indx = func(array, dim=dim, axis=None, keep_attrs=keep_attrs, skipna=skipna) | ||
|
||
# Get the coordinate we want. | ||
coordarray = array[dim] | ||
|
||
# Handle dask arrays. | ||
if isinstance(array, dask_array_type): | ||
res = dask_array.map_blocks(coordarray, indx, dtype=indx.dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two more things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You’re right, this is definitely broken. Anyone up for putting together a fix in a follow up PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
else: | ||
res = coordarray[ | ||
indx, | ||
] | ||
|
||
if skipna or (skipna is None and array.dtype.kind in na_dtypes): | ||
# Put the NaN values back in after removing them | ||
res = res.where(~allna, fill_value) | ||
|
||
# The dim is gone but we need to remove the corresponding coordinate. | ||
del res.coords[dim] | ||
|
||
# Copy attributes from argmin/argmax, if any | ||
res.attrs = indx.attrs | ||
|
||
return res |
Uh oh!
There was an error while loading. Please reload this page.