Hi all,

TL;DR:  Are there strong opinions on what:

   arr = np.array([None])
   arr[0] = np.array(3)
   arr.fill(np.array(3))

Should give?  Right now NumPy usually stores the 0-D array, I want to
make `arr.fill()` consistent, so that is what it would be doing also.


**Long discussion:**

NumPy has a bit of a consistency problem with item setting, that is,
what should happen if you have an array and set an individual element:

    arr = np.arange(10, dtype=object)  # object is most problematic
    arr[0] = value

Or similarly (but currently somewhat different):

    arr.fill(value)

I wish to make the behaviour more consistent here, that is, both of
these should use the same logic (fill is slightly different currently)
and that logic should be shared by all functionality that is based on
"setting a single element".


There is a tricky situation however if `value` is a 0-D array.  In most
cases, we can just copy the value over (using correct casting) as if we
copied an array:

    arr[0] = zero_d_arr

is the same as:

    arr[0, ...] = zero_d_arr  # definitely copies the array value

But this is currently *not* consistently the case.  I wish to make this
consistent.  The confusion is around object arrays, though:

    value = np.array(None, dtype=object)
    arr[0] = value

Stores `value` without unpacking it currently.

    arr.fill(value)

Stores the `None` (unpacking `value`) if and only if `value` is 0-D.

Right now, i aligned `fill` with the item assignment, but this could be
argued.

Further related behaviour is that:

    np.array(value)  # unpacks any array
    np.array([value, None], dtype=object)  # does not unpack

Which allows you to pack arrays into object arrays.

Now, we could unpack making it impossible to place a 0-D array into an
object array except via `arr.itemset()`.

My current preference is to store the 0-D arrays as-is, and basically
say that passing a 0-D array as value for:

    arr.fill(0d_arr)
    arr[0] = 0d_arr  # this is fine: arr[0, ...] = 0d_arr

it makes some issues around "array-like" objects a bit easier, and is
just what we do _most_ (but not all!) of the time.  But I know that
many will look at it and immediately say: No this has to be unpacked! 
So I am open to suggestions or changing this in general.
(However, I think aligning `arr.fill` is OK, changing `arr[0] = ...`, I
don't know.)

Cheers,

Sebastian

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to