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 as part of the 3.x series, 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 b3a47ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Expand Up @@ -10,6 +10,7 @@ Latest
- REF: use regex to process PROJ strings in :meth:`.CRS.to_dict` (pull #1086)
- BUG: :class:`.MercatorAConversion` defined only for lat_0 = 0 (issue #1089)
- BUG: Add support for `PROJ_DATA` environment variable (issue #1097)
- BUG: Ensure numpy masked arrays stay masked after projection (issue #1102)

3.3.1
-------
Expand Down
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 b3a47ca

Please sign in to comment.