Skip to content

Commit

Permalink
Ensure numpy masked arrays stay masked after projection
Browse files Browse the repository at this point in the history
As noted in pyproj4#1102, projecting numpy masked arrays returned numpy masked
arrays in the 2.x series of pyproj.  This behaviour changed in commit
4ab3ff7, where a "plain" numpy ndarray was returned.  The change
implemented here ensures that projecting numpy masked arrays returns
masked arrays as was previously the case.
  • Loading branch information
paultcochrane committed Jul 10, 2022
1 parent 8b0e5a2 commit ca573c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyproj/utils.py
Expand Up @@ -108,6 +108,11 @@ def _copytobuffer(xxx: Any, inplace: bool = False) -> Tuple[Any, DataType]:
Tuple[Any, DataType]
The copy of the data prepared for the PROJ API & Python Buffer API.
"""
# handle numpy masked Arrays; note that pandas.Series also has a "mask"
# attribute, hence checking for simply the "mask" attr isn't sufficient
if hasattr(xxx, "hardmask"):
return xxx, DataType.ARRAY

# check for pandas.Series, xarray.DataArray or dask.array.Array
if hasattr(xxx, "__array__") and callable(xxx.__array__):
xxx = xxx.__array__()
Expand Down
7 changes: 7 additions & 0 deletions test/test_utils.py
Expand Up @@ -42,6 +42,13 @@ def test__copytobuffer__numpy_array(in_arr):
)


def test__copytobuffer__numpy_masked_array():
in_arr = numpy.ma.array([1])
out_arr, dtype = _copytobuffer(in_arr)

assert isinstance(out_arr, numpy.ma.MaskedArray)


def test__copytobuffer__fortran_order():
data = numpy.ones((2, 4), dtype=numpy.float64, order="F")
converted_data, dtype = _copytobuffer(data)
Expand Down

0 comments on commit ca573c5

Please sign in to comment.