def expand_dims(x, axis):
result = n_expand_dims(x, axis)<EOL>if isinstance(x, MaskedArray):<EOL><INDENT>new_shape = result.shape<EOL>result = x.view()<EOL>result.shape = new_shape<EOL>if result._mask is not nomask:<EOL><INDENT>result._mask.shape = new_shape<EOL><DEDENT><DEDENT>return result<EOL>
Expand the shape of an array.

Expands the shape of the array by including a new axis before the one
specified by the `axis` parameter. This function behaves the same as
`numpy.expand_dims` but preserves masked elements.

See Also
--------
numpy.expand_dims : Equivalent function in top-level NumPy module.

Examples
--------
>>> import numpy.ma as ma
>>> x = ma.array([1, 2, 4])
>>> x[1] = ma.masked
>>> x
masked_array(data = [1 -- 4],
             mask = [False  True False],
       fill_value = 999999)
>>> np.expand_dims(x, axis=0)
array([[1, 2, 4]])
>>> ma.expand_dims(x, axis=0)
masked_array(data =
 [[1 -- 4]],
             mask =
 [[False  True False]],
       fill_value = 999999)

The same result can be achieved using slicing syntax with `np.newaxis`.

>>> x[np.newaxis, :]
masked_array(data =
 [[1 -- 4]],
             mask =
 [[False  True False]],
       fill_value = 999999)