1

I was looking for a way to convert a complex array to a 2D float array in place and found this:

> print np.empty(10, dtype='complex128').reshape(-1,1).view(dtype='float64').shape
> (10,2)

Interestingly, using [:,newaxis] to add the extra dimension fails:

> print np.empty(10, dtype='complex128')[:,np.newaxis].view(dtype='float64').shape
> ValueError: new type not compatible with array.

This related thread discusses that both reshape and newaxis create a view but there are differences in how the strides are set:

Numpy: use reshape or newaxis to add dimensions

How does this creating a float view on a complex array?

Community
  • 1
  • 1
  • 1
    What version of python / numpy do you use? Both work similar for me using `Python2.7.12` with `numpy1.12.0`. Also in your example you just wrote `newaxis` without referring to numpy aka `np`. Maybe `newaxis` is bound to something else in your code? – a_guest Feb 27 '17 at 12:30
  • @a_guest I'm on `numpy1.11.3`. Also, I corrected the code to include `np.` – user7629726 Feb 27 '17 at 14:20
  • Maybe that's a bug or they changed the way views of arrays work. Interestingly applying an additional `np.array` to the broadcasted array makes it work: `np.array(np.empty(10, dtype='complex128')[:,np.newaxis]).view(dtype='float64').shape`. I suggest upgrading to the latest release of numpy. – a_guest Feb 27 '17 at 15:34

0 Answers0