Skip to content
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 Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
array dtypes with 'SN' string subtypes can now be used to
define netcdf compound types (they get converted to ('S1',N)
character array types automatically).
* always return masked array by default, even if there are no
masked values (too surprising to get ndarray or MaskedArray depending
on slice, issue #785).


version 1.3.1 (tag v1.3.1rel)
Expand Down
24 changes: 16 additions & 8 deletions netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4269,16 +4269,24 @@ rename a `netCDF4.Variable` attribute named `oldname` to `newname`."""
totalmask += data > validmax
if fill_value is None and fval is not None:
fill_value = fval
# if all else fails, use default _FillValue as fill_value
# for masked array.
if fill_value is None:
fill_value = default_fillvals[self.dtype.str[1:]]
# create masked array with computed mask
if totalmask.any() and fill_value is not None:
if totalmask.any():
data = ma.masked_array(data,mask=totalmask,fill_value=fill_value)
# issue 515 scalar array with mask=True should be converted
# to numpy.ma.MaskedConstant to be consistent with slicing
# behavior of masked arrays.
if data.shape == () and data.mask.all():
# return a scalar numpy masked constant not a 0-d masked array,
# so that data == numpy.ma.masked.
data = data[()] # changed from [...] (issue #662)
else:
# issue #785: always return masked array, if no values masked
# set mask=False.
data = ma.masked_array(data,mask=False,fill_value=fill_value)
# issue 515 scalar array with mask=True should be converted
# to numpy.ma.MaskedConstant to be consistent with slicing
# behavior of masked arrays.
if data.shape == () and data.mask.all():
# return a scalar numpy masked constant not a 0-d masked array,
# so that data == numpy.ma.masked.
data = data[()] # changed from [...] (issue #662)
return data

def _assign_vlen(self, elem, data):
Expand Down
4 changes: 3 additions & 1 deletion test/tst_fancyslicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def test_get(self):
# Empty boolean -- all False
d1 = f.variables['data1']
m = np.zeros(xdim, bool)
assert_equal(d1[m], ())
if np.__version__ > '1.9.0':
# fails for old numpy versions
assert_equal(d1[m], ())

# Check that no assignment is made
d1[m] = 0
Expand Down
6 changes: 4 additions & 2 deletions test/tst_scaled.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def test_unmasked(self):

self.assertEqual(v.dtype, "i2")
self.assertTrue(isinstance(v, np.ndarray))
self.assertTrue(not isinstance(v, ma.core.MaskedArray))
# issue 785: always return masked array by default
self.assertTrue(isinstance(v, ma.core.MaskedArray))
assert_array_almost_equal(v, self.v)

f.close()
Expand Down Expand Up @@ -107,7 +108,8 @@ def test_unmasked(self):

self.assertEqual(v_scaled.dtype, "f8")
self.assertTrue(isinstance(v_scaled, np.ndarray))
self.assertTrue(not isinstance(v_scaled, ma.core.MaskedArray))
# issue 785: always return masked array by default
self.assertTrue(isinstance(v_scaled, ma.core.MaskedArray))
assert_array_almost_equal(v_scaled, self.v_scaled)
f.close()

Expand Down
4 changes: 4 additions & 0 deletions test/tst_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def test_0d(self):
v[...] = 10
assert_array_equal(v[...], 10)
assert_equal(v.shape, v[...].shape)
# issue #785: always return masked array
#assert(type(v[...]) == np.ndarray)
assert(type(v[...]) == np.ma.core.MaskedArray)
f.set_auto_mask(False)
assert(type(v[...]) == np.ndarray)
f.close()

Expand Down