Skip to content

Commit

Permalink
Better error message for vectorize=True in apply_ufunc with old numpy (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
shoyer authored Mar 7, 2018
1 parent 54468e1 commit 870e4ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Enhancements
Bug fixes
~~~~~~~~~

- Raise an informative error message when using ``apply_ufunc`` with numpy
v1.11 (:issue:`1956`).
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Fix the precision drop after indexing datetime64 arrays (:issue:`1932`).
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Fix kwarg `colors` clashing with auto-inferred `cmap` (:issue:`1461`)
Expand Down
23 changes: 17 additions & 6 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Functions for applying functions that act on arrays to xarray's labeled data.
NOT PUBLIC API.
"""
from __future__ import absolute_import, division, print_function
from distutils.version import LooseVersion
import functools
import itertools
import operator
Expand Down Expand Up @@ -882,10 +882,21 @@ def earth_mover_distance(first_samples,
func = functools.partial(func, **kwargs_)

if vectorize:
func = np.vectorize(func,
otypes=output_dtypes,
signature=signature.to_gufunc_string(),
excluded=set(kwargs))
if signature.all_core_dims:
# we need the signature argument
if LooseVersion(np.__version__) < '1.12': # pragma: no cover
raise NotImplementedError(
'numpy 1.12 or newer required when using vectorize=True '
'in xarray.apply_ufunc with non-scalar output core '
'dimensions.')
func = np.vectorize(func,
otypes=output_dtypes,
signature=signature.to_gufunc_string(),
excluded=set(kwargs))
else:
func = np.vectorize(func,
otypes=output_dtypes,
excluded=set(kwargs))

variables_ufunc = functools.partial(apply_variable_ufunc, func,
signature=signature,
Expand Down

0 comments on commit 870e4ea

Please sign in to comment.