Skip to content

Commit

Permalink
ENH: Add support for xarray & dask arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed May 7, 2020
1 parent f315650 commit f86591b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[settings]
line_length=88
multi_line_output=3
known_third_party=mock,numpy,pkg_resources,pytest,setuptools
known_third_party=mock,numpy,pkg_resources,pytest,setuptools,xarray
known_first_party=pyproj,test
include_trailing_comma=true
10 changes: 8 additions & 2 deletions pyproj/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _copytobuffer(xx: Any) -> Tuple[Any, bool, bool, bool]:
isfloat = False
islist = False
istuple = False
# first, if it's a numpy array scalar convert to float
# if it's a numpy array scalar convert to float
# (array scalars don't support buffer API)
if hasattr(xx, "shape"):
if xx.shape == ():
Expand All @@ -49,14 +49,20 @@ def _copytobuffer(xx: Any) -> Tuple[Any, bool, bool, bool]:
return inx, False, False, False
except Exception:
try: # perhaps they are Numeric/numarrays?
# check for xarray or dask array
if hasattr(xx, "__array__") and callable(xx.__array__):
return _copytobuffer(xx.__array__(dtype="d"))
# sorry, not tested yet.
# i don't know Numeric/numarrays has `shape'.
xx.typecode()
inx = xx.astype("d")
# inx,isfloat,islist,istuple
return inx, False, False, False
except Exception:
raise TypeError("input must be an array, list, tuple or scalar")
raise TypeError(
"input must be an array, list, tuple, scalar, "
"or have the __array__ method."
)
else:
# perhaps they are regular python arrays?
if hasattr(xx, "typecode"):
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cython>=0.28.4
black; python_version >= '3.6'
black
flake8
mock
mypy
Expand All @@ -9,3 +9,4 @@ pytest>3.6
pytest-cov
shapely
pre-commit
xarray
7 changes: 7 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy
import pytest
from xarray import DataArray

from pyproj.utils import _copytobuffer, _copytobuffer_return_scalar

Expand All @@ -20,6 +21,7 @@ def test__copytobuffer_return_scalar__invalid():
"in_data, is_float, is_list, is_tuple",
[
(numpy.array(1), True, False, False),
(DataArray(numpy.array(1)), True, False, False),
(1, True, False, False),
([1], False, True, False),
((1,), False, False, True),
Expand All @@ -34,6 +36,11 @@ def test__copytobuffer__numpy_array():
assert _copytobuffer(in_arr) == (in_arr.astype("d"), False, False, False)


def test__copytobuffer__xarray_dataarray():
in_arr = DataArray([1])
assert _copytobuffer(in_arr) == (in_arr.__array__(dtype="d"), False, False, False)


def test__copytobuffer__invalid():
with pytest.raises(TypeError):
_copytobuffer("invalid")

0 comments on commit f86591b

Please sign in to comment.