8000 * Force _fill_value to a 0d array (so that field values can be propag… · numpy/numpy@f8f753b · GitHub
[go: up one dir, main page]

Skip to content

Commit f8f753b

Browse files
author
pierregm
committed
* Force _fill_value to a 0d array (so that field values can be propagated, bug #1332)
* Make .fill_value return a scalar (int, float, void...) and not a 0d array
1 parent 2e28be8 commit f8f753b

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

numpy/ma/core.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
'zeros']
6969

7070
import cPickle
71-
import operator
7271

7372
import numpy as np
7473
from numpy import ndarray, amax, amin, iscomplexobj, bool_
@@ -108,12 +107,12 @@ def get_object_signature(obj):
108107
try:
109108
sig = formatargspec(*getargspec(obj))
110109
except TypeError, errmsg:
111-
msg = "Unable to retrieve the signature of %s '%s'\n"\
112-
"(Initial error message: %s)"
110+
sig = ''
111+
# msg = "Unable to retrieve the signature of %s '%s'\n"\
112+
# "(Initial error message: %s)"
113113
# warnings.warn(msg % (type(obj),
114114
# getattr(obj, '__name__', '???'),
115115
# errmsg))
116-
sig = ''
117116
return sig
118117

119118

@@ -411,10 +410,10 @@ def _check_fill_value(fill_value, ndtype):
411410
else:
412411
# In case we want to convert 1e+20 to int...
413412
try:
414-
fill_value = np.array(fill_value, copy=False, dtype=ndtype).item()
413+
fill_value = np.array(fill_value, copy=False, dtype=ndtype)#.item()
415414
except OverflowError:
416415
fill_value = default_fill_value(ndtype)
417-
return fill_value
416+
return np.array(fill_value)
418417

419418

420419
def set_fill_value(a, fill_value):
@@ -478,7 +477,7 @@ def set_fill_value(a, fill_value):
478477
479478
"""
480479
if isinstance(a, MaskedArray):
481-
a._fill_value = _check_fill_value(fill_value, a.dtype)
480+
a.set_fill_value(fill_value)
482481
return
483482

484483
def get_fill_value(a):
@@ -3259,23 +3258,21 @@ def raw_data(self):
32593258
"""
32603259
warnings.warn('Use .data instead.', DeprecationWarning)
32613260
return self._data
3262-
#............................................
3263-
def _get_flat(self):
3264-
"""Return a flat iterator.
32653261

3266-
"""
3262+
3263+
def _get_flat(self):
3264+
"Return a flat iterator."
32673265
return MaskedIterator(self)
32683266
#
32693267
def _set_flat (self, value):
3270-
"""Set a flattened version of self to value.
3271-
3272-
"""
3268+
"Set a flattened version of self to value."
32733269
y = self.ravel()
32743270
y[:] = value
32753271
#
32763272
flat = property(fget=_get_flat, fset=_set_flat,
32773273
doc="Flat version of the array.")
3278-
#............................................
3274+
3275+
32793276
def get_fill_value(self):
32803277
"""
32813278
Return the filling value of the masked array.
@@ -3302,7 +3299,7 @@ def get_fill_value(self):
33023299
"""
33033300
if self._fill_value is None:
33043301
self._fill_value = _check_fill_value(None, self.dtype)
3305-
return self._fill_value
3302+
return self._fill_value[()]
33063303

33073304
def set_fill_value(self, value=None):
33083305
"""
@@ -3334,7 +3331,14 @@ def set_fill_value(self, value=None):
33343331
1e+20
33353332
33363333
"""
3337-
self._fill_value = _check_fill_value(value, self.dtype)
3334+
target = _check_fill_value(value, self.dtype)
3335+
_fill_value = self._fill_value
3336+
if _fill_value is None:
3337+
# Create the attribute if it was undefined
3338+
self._fill_value = target
3339+
else:
3340+
# Don't overwrite the attribute, just fill it (for propagation)
3341+
_fill_value[()] = target
33383342

33393343
fill_value = property(fget=get_fill_value, fset=set_fill_value,
33403344
doc="Filling value.")
@@ -5400,7 +5404,7 @@ def __setstate__(self, state):
54005404
- a binary string for the mask.
54015405
54025406
"""
5403-
(ver, shp, typ, isf, raw, msk, flv) = state
5407+
(_, shp, typ, isf, raw, msk, flv) = state
54045408
ndarray.__setstate__(self, (shp, typ, isf, raw))
54055409
self._mask.__setstate__((shp, make_mask_descr(typ), isf, msk))
54065410
self.fill_value = flv
@@ -5525,7 +5529,14 @@ def filled(self, fill_value=None):
55255529

55265530
def tolist(self):
55275531
"""
5528-
Transforms the object into a list
5532+
Transforms the mvoid object into a tuple.
5533+
5534+
Masked fields are replaced by None.
5535+
5536+
Returns
5537+
-------
5538+
returned_tuple
5539+
Tuple of fields
55295540
"""
55305541
_mask = self._mask
55315542
if _mask is nomask:

numpy/ma/tests/test_core.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,13 @@ def test_check_on_fields(self):
12971297
fval = _check_fill_value([-999, -999.9, "???"], ndtype)
12981298
self.failUnless(isinstance(fval, ndarray))
12991299
assert_equal(fval.item(), [-999, -999.9, "???"])
1300-
# A check on Non should output the defaults
1300+
# A check on None should output the defaults
13011301
fval = _check_fill_value(None, ndtype)
13021302
self.failUnless(isinstance(fval, ndarray))
13031303
assert_equal(fval.item(), [default_fill_value(0),
13041304
default_fill_value(0.),
13051305
default_fill_value("0")])
1306-
#.....Using a flexible type as fill_value should work
1306+
#.....Using a structured type as fill_value should work
13071307
fill_val = np.array((-999, -999.9, "???"), dtype=ndtype)
13081308
fval = _check_fill_value(fill_val, ndtype)
13091309
self.failUnless(isinstance(fval, ndarray))
@@ -1378,6 +1378,7 @@ def test_fillvalue(self):
13781378
x.fill_value = 999
13791379
assert_equal(np.asarray(x.fill_value).dtype, float)
13801380
assert_equal(x.fill_value, 999.)
1381+
assert_equal(x._fill_value, np.array(999.))
13811382

13821383

13831384
def test_fillvalue_exotic_dtype(self):
@@ -1429,6 +1430,18 @@ def test_extremum_fill_value(self):
14291430
assert_equal(test[1][1], maximum_fill_value(a['B']['BB']))
14301431
assert_equal(test[1], maximum_fill_value(a['B']))
14311432

1433+
def test_fillvalue_individual_fields(self):
1434+
"Test setting fill_value on individual fields"
1435+
ndtype = [('a', int), ('b', int)]
1436+
a = array(zip([1, 2, 3], [4, 5, 6]),
1437+
fill_value=(-999, -999), dtype=ndtype)
1438+
f = a._fill_value
1439+
aa = a['a']
1440+
aa.set_fill_value(10)
1441+
assert_equal(aa._fill_value, np.array(10))
1442+
assert_equal(tuple(a.fill_value), (10, -999))
1443+
a.fill_value['b'] = -10
1444+
assert_equal(tuple(a.fill_value), (10, -10))
14321445

14331446
#------------------------------------------------------------------------------
14341447

0 commit comments

Comments
 (0)
0