Convert the input to a masked array, conserving subclasses. If `a` is a subclass of `MaskedArray`, its class is conserved. No copy is performed if the input is already an `ndarray`. Parameters ---------- a : array_like Input data, in any form that can be converted
(a, dtype=None)
| 8251 | |
| 8252 | |
| 8253 | def asanyarray(a, dtype=None): |
| 8254 | """ |
| 8255 | Convert the input to a masked array, conserving subclasses. |
| 8256 | |
| 8257 | If `a` is a subclass of `MaskedArray`, its class is conserved. |
| 8258 | No copy is performed if the input is already an `ndarray`. |
| 8259 | |
| 8260 | Parameters |
| 8261 | ---------- |
| 8262 | a : array_like |
| 8263 | Input data, in any form that can be converted to an array. |
| 8264 | dtype : dtype, optional |
| 8265 | By default, the data-type is inferred from the input data. |
| 8266 | order : {'C', 'F'}, optional |
| 8267 | Whether to use row-major ('C') or column-major ('FORTRAN') memory |
| 8268 | representation. Default is 'C'. |
| 8269 | |
| 8270 | Returns |
| 8271 | ------- |
| 8272 | out : MaskedArray |
| 8273 | MaskedArray interpretation of `a`. |
| 8274 | |
| 8275 | See Also |
| 8276 | -------- |
| 8277 | asarray : Similar to `asanyarray`, but does not conserve subclass. |
| 8278 | |
| 8279 | Examples |
| 8280 | -------- |
| 8281 | >>> x = np.arange(10.).reshape(2, 5) |
| 8282 | >>> x |
| 8283 | array([[0., 1., 2., 3., 4.], |
| 8284 | [5., 6., 7., 8., 9.]]) |
| 8285 | >>> np.ma.asanyarray(x) |
| 8286 | masked_array( |
| 8287 | data=[[0., 1., 2., 3., 4.], |
| 8288 | [5., 6., 7., 8., 9.]], |
| 8289 | mask=False, |
| 8290 | fill_value=1e+20) |
| 8291 | >>> type(np.ma.asanyarray(x)) |
| 8292 | <class 'numpy.ma.core.MaskedArray'> |
| 8293 | |
| 8294 | """ |
| 8295 | # workaround for #8666, to preserve identity. Ideally the bottom line |
| 8296 | # would handle this for us. |
| 8297 | if isinstance(a, MaskedArray) and (dtype is None or dtype == a.dtype): |
| 8298 | return a |
| 8299 | return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True) |
| 8300 | |
| 8301 | |
| 8302 | ############################################################################## |
no outgoing calls