No way to specify compat behaviour for xr.apply_ufunc and ops that use it; binary arithmetic ufuncs don't use arithmetic_compat.

Abierto
#11,272 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
42/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
numpy, python
Área
api, data

Línea de trabajo

Comienza con xr.apply_ufunc y las rutas de ufunc con múltiples argumentos descritas en el issue, incluidas _binary_ufunc, xr.dot, xr.where, da.where y da.fillna. Compara cómo se selecciona actualmente compat con los operadores aritméticos e identifica las pruebas o los llamadores afectados. Se considera terminado cuando el comportamiento compat explícito o basado en opciones elegido se aplica de forma coherente sin cambiar otros valores predeterminados.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement
Is your feature request related to a problem?

We're now able to override the problematic (see https://github.com/pydata/xarray/issues/9481, https://github.com/pydata/xarray/issues/10924) compat='minimal' default for arithmetic using e.g. xr.set_options(arithmetic_compat='override'):

import xarray as xr
a = xr.DataArray(1, coords={'foo': 1})
b = xr.DataArray(1, coords={'foo': 2})

with xr.set_options(arithmetic_compat='override'):
     print(a + b)

<xarray.DataArray ()> Size: 8B
array(2)
Coordinates:
    foo      int64 8B 1

Unfortunately there's no way to set compat for xr.apply_ufunc, or for any of the multi-argument ufuncs based on it which perform compat checks

with xr.set_options(arithmetic_compat='override'):
     print(xr.apply_ufunc(lambda x, y: x+y, a, b))

<xarray.DataArray ()> Size: 8B
array(2)

with xr.set_options(arithmetic_compat='override'):
     print(xr.where(True, a, b))

<xarray.DataArray ()> Size: 8B
array(1)

with xr.set_options(arithmetic_compat='override'):
     print(xr.ufuncs.add(a, b))

<xarray.DataArray ()> Size: 8B
array(1)

This is particularly problematic because the behavior now differs for arithmetic operators (going via _binary_op) and arithmetic ufuncs like xr.ufuncs.add or np.add, which go via apply_ufunc.

Describe the solution you'd like / alternatives you've considered

Any views on these options:

(A) : Add an explicit compat argument to apply_ufunc, defaulting to the current behaviour of 'minimal', and let multi-argument ufuncs pass this argument on. (xr.ufuncs based on _binary_ufunc already pass through extra kwargs, but some like xr.dot, xr.where, da.where, da.fillna would need updating.)

(B): Make it configurable via xr.set_options. It could share the existing arithmetic_compat setting which defaults to minimal, or add a new ufunc_compat setting.

(C): (A)+(B): there's an explicit argument for it but it defaults to e.g. OPTIONS["ufunc_compat"]

In all cases I'd suggest setting the defaults to change in a future version to 'override', along with the other planned compat default changes.

If we used the arithmetic_compat setting for apply_ufunc, it'd be reasonable to ask why not for consistency also use arithmetic_join as the default for apply_ufunc's join. However its default is currently different (exact for apply_ufunc, inner for arithmetic), so maybe using a separate ufunc_compat side-steps this.

Lenguaje dominante
Python
Estrellas
4.2k
Forks
1.5k
Merge medio
2 d 21 h
PR fusionados (30 d)
74

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pydata/xarray

Todos los issues de pydata/xarray

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.