How to save and load an array of complex numbers using numpy.savetxt?

Posted by ptomato on Stack Overflow See other posts from Stack Overflow or by ptomato
Published on 2011-06-27T14:04:11Z Indexed on 2011/06/29 16:22 UTC
Read the original article Hit count: 206

Filed under:
|
|

I want to use numpy.savetxt() to save an array of complex numbers to a text file. Problems:

  • If you save the complex array with the default format string, the imaginary part is discarded.
  • If you use fmt='%s', then numpy.loadtxt() can't load it unless you specify dtype=complex, converters={0: lambda s: complex(s)}. Even then, if there are NaN's in the array, loading still fails.

It looks like someone has inquired about this multiple times on the Numpy mailing list and even filed a bug, but has not gotten a response. Before I put something together myself, is there a canonical way to do this?

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy