Skip to content

Better error message for vectorize=True in apply_ufunc with old numpy #1963

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

Merged
merged 4 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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>`_.

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