MCPcopy Create free account
hub / github.com/numpy/numpy / __new__

Method __new__

numpy/ma/core.py:2808–2949  ·  view source on GitHub ↗

Create a new masked array from scratch. Notes ----- A masked array can also be created by taking a .view(MaskedArray).

(cls, data=None, mask=nomask, dtype=None, copy=False,
                subok=True, ndmin=0, fill_value=None, keep_mask=True,
                hard_mask=None, shrink=True, order=None)

Source from the content-addressed store, hash-verified

2806 _print_width_1d = 1500
2807
2808 def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
2809 subok=True, ndmin=0, fill_value=None, keep_mask=True,
2810 hard_mask=None, shrink=True, order=None):
2811 """
2812 Create a new masked array from scratch.
2813
2814 Notes
2815 -----
2816 A masked array can also be created by taking a .view(MaskedArray).
2817
2818 """
2819 # Process data.
2820 _data = np.array(data, dtype=dtype, copy=copy,
2821 order=order, subok=True, ndmin=ndmin)
2822 _baseclass = getattr(data, '_baseclass', type(_data))
2823 # Check that we're not erasing the mask.
2824 if isinstance(data, MaskedArray) and (data.shape != _data.shape):
2825 copy = True
2826
2827 # Here, we copy the _view_, so that we can attach new properties to it
2828 # we must never do .view(MaskedConstant), as that would create a new
2829 # instance of np.ma.masked, which make identity comparison fail
2830 if isinstance(data, cls) and subok and not isinstance(data, MaskedConstant):
2831 _data = ndarray.view(_data, type(data))
2832 else:
2833 _data = ndarray.view(_data, cls)
2834
2835 # Handle the case where data is not a subclass of ndarray, but
2836 # still has the _mask attribute like MaskedArrays
2837 if hasattr(data, '_mask') and not isinstance(data, ndarray):
2838 _data._mask = data._mask
2839 # FIXME: should we set `_data._sharedmask = True`?
2840 # Process mask.
2841 # Type of the mask
2842 mdtype = make_mask_descr(_data.dtype)
2843 if mask is nomask:
2844 # Case 1. : no mask in input.
2845 # Erase the current mask ?
2846 if not keep_mask:
2847 # With a reduced version
2848 if shrink:
2849 _data._mask = nomask
2850 # With full version
2851 else:
2852 _data._mask = np.zeros(_data.shape, dtype=mdtype)
2853 # Check whether we missed something
2854 elif isinstance(data, (tuple, list)):
2855 try:
2856 # If data is a sequence of masked array
2857 mask = np.array(
2858 [getmaskarray(np.asanyarray(m, dtype=_data.dtype))
2859 for m in data], dtype=mdtype)
2860 except (ValueError, TypeError):
2861 # If data is nested
2862 mask = nomask
2863 # Force shrinking of the mask if needed (and possible)
2864 if (mdtype == MaskType) and mask.any():
2865 _data._mask = mask

Callers

nothing calls this directly

Calls 10

make_mask_descrFunction · 0.85
getmaskarrayFunction · 0.85
getmaskFunction · 0.85
MaskErrorClass · 0.85
_check_fill_valueFunction · 0.85
reshapeMethod · 0.80
viewMethod · 0.45
anyMethod · 0.45
copyMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected