[Numpy-discussion] changing 'nan' string for np.nan in np.savetxt

Maria Liukis liukis@usc....
Fri Nov 19 18:09:56 CST 2010


Hello everybody,

I was wondering if there is an elegant way of overwriting 'nan' string representation with 'NaN' when saving numpy array containing np.nan values with numpy.savetxt() function. numpy.set_printoptions(nanstr='NaN') setting (which has default value of 'NaN' already) does not seem to have any effect when writing array to the file, only when printing to the screen:

$ python
Python 2.5.2 (r252:60911, Sep 30 2008, 15:42:03) 
[GCC 4.3.2 20080917 (Red Hat 4.3.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> print np.__version__
1.3.0
>>> np.get_printoptions()
{'infstr': 'Inf', 'threshold': 1000, 'suppress': False, 'linewidth': 75, 'edgeitems': 3, 'precision': 8, 'nanstr': 'NaN'}
>>> 
>>> a = np.array([1, 2, 3, np.nan])
>>> a
array([  1.,   2.,   3.,  NaN])
>>> np.savetxt('testa.txt', a)
>>> 

$ cat testa.txt 
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
nan
$

Many thanks,
Masha


More information about the NumPy-Discussion mailing list