@@ -1580,6 +1580,16 @@ def is_mask(m):
1580
1580
return False
1581
1581
1582
1582
1583
+ def _shrink_mask (m ):
1584
+ """
1585
+ Shrink a mask to nomask if possible
1586
+ """
1587
+ if not m .dtype .names and not m .any ():
1588
+ return nomask
1589
+ else :
1590
+ return m
1591
+
1592
+
1583
1593
def make_mask (m , copy = False , shrink = True , dtype = MaskType ):
1584
1594
"""
1585
1595
Create a boolean mask from an array.
@@ -1659,10 +1669,9 @@ def make_mask(m, copy=False, shrink=True, dtype=MaskType):
1659
1669
# Fill the mask in case there are missing data; turn it into an ndarray.
1660
1670
result = np .array (filled (m , True ), copy = copy , dtype = dtype , subok = True )
1661
1671
# Bas les masques !
1662
- if shrink and (not result .dtype .names ) and (not result .any ()):
1663
- return nomask
1664
- else :
1665
- return result
1672
+ if shrink :
1673
+ result = _shrink_mask (result )
1674
+ return result
1666
1675
1667
1676
1668
1677
def make_mask_none (newshape , dtype = None ):
@@ -1949,7 +1958,7 @@ def masked_where(condition, a, copy=True):
1949
1958
1950
1959
"""
1951
1960
# Make sure that condition is a valid standard-type mask.
1952
- cond = make_mask (condition )
1961
+ cond = make_mask (condition , shrink = False )
1953
1962
a = np .array (a , copy = copy , subok = True )
1954
1963
1955
1964
(cshape , ashape ) = (cond .shape , a .shape )
@@ -1963,7 +1972,7 @@ def masked_where(condition, a, copy=True):
1963
1972
cls = MaskedArray
1964
1973
result = a .view (cls )
1965
1974
# Assign to *.mask so that structured masks are handled correctly.
1966
- result .mask = cond
1975
+ result .mask = _shrink_mask ( cond )
1967
1976
return result
1968
1977
1969
1978
@@ -3607,9 +3616,7 @@ def shrink_mask(self):
3607
3616
False
3608
3617
3609
3618
"""
3610
- m = self ._mask
3611
- if m .ndim and not m .any ():
3612
- self ._mask = nomask
3619
+ self ._mask = _shrink_mask (self ._mask )
3613
3620
return self
3614
3621
3615
3622
baseclass = property (fget = lambda self : self ._baseclass ,
@@ -6709,12 +6716,11 @@ def concatenate(arrays, axis=0):
6709
6716
return data
6710
6717
# OK, so we have to concatenate the masks
6711
6718
dm = np .concatenate ([getmaskarray (a ) for a in
10000
arrays ], axis )
6719
+ dm = dm .reshape (d .shape )
6720
+
6712
6721
# If we decide to keep a '_shrinkmask' option, we want to check that
6713
6722
# all of them are True, and then check for dm.any()
6714
- if not dm .dtype .fields and not dm .any ():
6715
- data ._mask = nomask
6716
- else :
6717
- data ._mask = dm .reshape (d .shape )
6723
+ data ._mask = _shrink_mask (dm )
6718
6724
return data
6719
6725
6720
6726
@@ -7132,8 +7138,7 @@ def where(condition, x=_NoValue, y=_NoValue):
7132
7138
mask = np .where (cm , np .ones ((), dtype = mask .dtype ), mask )
7133
7139
7134
7140
# collapse the mask, for backwards compatibility
7135
- if mask .dtype == np .bool_ and not mask .any ():
7136
- mask = nomask
7141
+ mask = _shrink_mask (mask )
7137
7142
7138
7143
return masked_array (data , mask = mask )
7139
7144
0 commit comments