Skip to content

Commit 870e4ea

Browse files
authored
Better error message for vectorize=True in apply_ufunc with old numpy (#1963)
* Better error message for vectorize=True in apply_ufunc with old numpy * Typo otype -> otypes * add missing __future__ imports * all_output_core_dims -> all_core_dims
1 parent 54468e1 commit 870e4ea

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Enhancements
5757
Bug fixes
5858
~~~~~~~~~
5959

60+
- Raise an informative error message when using ``apply_ufunc`` with numpy
61+
v1.11 (:issue:`1956`).
62+
By `Stephan Hoyer <https://github.com/shoyer>`_.
6063
- Fix the precision drop after indexing datetime64 arrays (:issue:`1932`).
6164
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
6265
- Fix kwarg `colors` clashing with auto-inferred `cmap` (:issue:`1461`)

xarray/core/computation.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
Functions for applying functions that act on arrays to xarray's labeled data.
3-
4-
NOT PUBLIC API.
53
"""
4+
from __future__ import absolute_import, division, print_function
5+
from distutils.version import LooseVersion
66
import functools
77
import itertools
88
import operator
@@ -882,10 +882,21 @@ def earth_mover_distance(first_samples,
882882
func = functools.partial(func, **kwargs_)
883883

884884
if vectorize:
885-
func = np.vectorize(func,
886-
otypes=output_dtypes,
887-
signature=signature.to_gufunc_string(),
888-
excluded=set(kwargs))
885+
if signature.all_core_dims:
886+
# we need the signature argument
887+
if LooseVersion(np.__version__) < '1.12': # pragma: no cover
888+
raise NotImplementedError(
889+
'numpy 1.12 or newer required when using vectorize=True '
890+
'in xarray.apply_ufunc with non-scalar output core '
891+
'dimensions.')
892+
func = np.vectorize(func,
893+
otypes=output_dtypes,
894+
signature=signature.to_gufunc_string(),
895+
excluded=set(kwargs))
896+
else:
897+
func = np.vectorize(func,
898+
otypes=output_dtypes,
899+
excluded=set(kwargs))
889900

890901
variables_ufunc = functools.partial(apply_variable_ufunc, func,
891902
signature=signature,

0 commit comments

Comments
 (0)