[NumPy-Tickets] [NumPy] #2227: Array created with broadcast_array does not support indexing

NumPy Trac numpy-tickets@scipy....
Mon Oct 15 16:51:41 CDT 2012


#2227: Array created with broadcast_array does not support indexing
-------------------------+--------------------------------------------------
  Reporter:  timcera     |       Owner:  somebody   
      Type:  defect      |      Status:  closed     
  Priority:  normal      |   Milestone:  Unscheduled
 Component:  numpy.core  |     Version:  1.6.2      
Resolution:  wontfix     |    Keywords:             
-------------------------+--------------------------------------------------
Changes (by rkern):

  * status:  new => closed
  * resolution:  => wontfix


Old description:

> {{{
> >>> np.__version__
> '1.8.0.dev-d569ebc'
>
> >>> x = 2
>
> >>> skew = [0, 0.1, 0.2]
>
> >>> ans, x, skew = np.broadcast_arrays([1.0], x, skew)
>
> >>> ans
> array([ 1.,  1.,  1.])
>
> >>> mask = np.absolute(skew) < 0.001
>
> >>> mask
> array([ True, False, False], dtype=bool)
>
> >>> ans[mask] = 5.1
>
> >>> ans
> array([ 5.1,  5.1,  5.1])
> }}}
>

> The ans variable should be array([ 5.1,  1.0,  1.0])
>
> Doesn't work with integer indexes either...
>

> {{{
> >>> ans[2] = 9.3
>
> >>> ans
> array([ 9.3,  9.3,  9.3])
>
> }}}
>
> Should be array([ 1.0,  1.0,  9.3])
>

> {{{
> >>> goodans = np.ones(3.0)
>
> >>> goodans
> array([ 1.,  1.,  1.])
>
> >>> goodans[mask] = 5.4
>
> >>> goodans
> array([ 5.4,  1.0,  1.0])
> }}}
>

> If you deep copy the 'ans' indexing works again...
>

> {{{
> >>> copyans = ans.copy()
>
> >>> copyans[0] = 6.7
>
> >>> copyans
> array([ 6.7,  1. ,  1. ])
> }}}
>

> Noticed that there were differences in .base and .strides between the
> broadcast_arrays and the copied arrays/np.ones created arrays.

New description:

 {{{
 >>> np.__version__
 '1.8.0.dev-d569ebc'

 >>> x = 2

 >>> skew = [0, 0.1, 0.2]

 >>> ans, x, skew = np.broadcast_arrays([1.0], x, skew)

 >>> ans
 array([ 1.,  1.,  1.])

 >>> mask = np.absolute(skew) < 0.001

 >>> mask
 array([ True, False, False], dtype=bool)

 >>> ans[mask] = 5.1

 >>> ans
 array([ 5.1,  5.1,  5.1])
 }}}


 The ans variable should be array([ 5.1,  1.0,  1.0])

 Doesn't work with integer indexes either...


 {{{
 >>> ans[2] = 9.3

 >>> ans
 array([ 9.3,  9.3,  9.3])

 }}}

 Should be array([ 1.0,  1.0,  9.3])


 {{{
 >>> goodans = np.ones(3.0)

 >>> goodans
 array([ 1.,  1.,  1.])

 >>> goodans[mask] = 5.4

 >>> goodans
 array([ 5.4,  1.0,  1.0])
 }}}


 If you deep copy the 'ans' indexing works again...


 {{{
 >>> copyans = ans.copy()

 >>> copyans[0] = 6.7

 >>> copyans
 array([ 6.7,  1. ,  1. ])
 }}}


 Noticed that there were differences in .base and .strides between the
 broadcast_arrays and the copied arrays/np.ones created arrays.

--

Comment:

 This is the intended behavior. Note the "Returns" section of the
 docstring.

 http://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_arrays.html

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/2227#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list