'How to pretty print masked values in an numpy.ma array?

I have the following code :

import numpy as np
aa  = np.ma.masked_invalid ( [[1., 2.], [np.nan, 4.0]])
fmt = '{:12.4f} {:12.4f}'
print ( fmt.format ( aa[0,0], aa[0,1] ))
print ( fmt.format ( aa[1,0], aa[1,1] ))

and the result is :

      1.0000       2.0000
--       4.0000

There is also a warning : FutureWarning: Format strings passed to MaskedConstant are ignored, but in future may error or produce different behavior

The masked value breaks the padding and the alignement.

Is there a way to keep the padding and the alignement when printing masked values ?

Thanks,

Olivier



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source