[Numpy-discussion] Re: Unpacking 0-D object arrays when setting item/filling

2022-02-15 Thread Matti Picus



On 15/2/22 09:53, Stefan van der Walt wrote:

On Mon, Feb 14, 2022, at 12:45, Sebastian Berg wrote:

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.

That last behavior doesn't look right to me.  An object array should be thought 
of as a collection of pointers, and if you happen to want to point to a NumPy 
array, so be it.
I think we should strive for consistency and code simplicity. In the 
non-object case, it is clear that assignment will try to unpack an 
ndarray. So we should do the same thing with object arrays, and document 
the change in behaviour. Could we suggest a backward compatible 
alternative (would using a record dtype fit better with Stefan's mental 
model)?


Matti


___
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


[Numpy-discussion] Re: Unpacking 0-D object arrays when setting item/filling

2022-02-15 Thread Sebastian Berg
On Tue, 2022-02-15 at 10:21 +0200, Matti Picus wrote:
> 
> On 15/2/22 09:53, Stefan van der Walt wrote:
> > On Mon, Feb 14, 2022, at 12:45, Sebastian Berg wrote:
> > > 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.
> > That last behavior doesn't look right to me.  An object array
> > should be thought of as a collection of pointers, and if you happen
> > to want to point to a NumPy array, so be it.
> I think we should strive for consistency and code simplicity. In the 
> non-object case, it is clear that assignment will try to unpack an 
> ndarray. So we should do the same thing with object arrays, and
> document 
> the change in behaviour. Could we suggest a backward compatible 
> alternative (would using a record dtype fit better with Stefan's
> mental 
> model)?

A possible workaround might be `arr.itemset()`, although I am not quite
convinced that it should do that.
A structured array could behave differently maybe, but presumably:

arr = np.array([1], "i,i")  # structured
arr[0] = (np.array(3), np.array(3))

Should also unpack (with correct casting, not via `__int__`).

I guess we may have less special cases if we just unpack 0-D and _try_
to always pack N-D arrays with N>0 (which will often fail).

I have to admit, I don't care too much about that special case.  The
point is that if done right these special cases should be confined to
two places:

1. Discovering the dimension/dtype in `np.asarray(nested_objects)`
2. The `PyArray_Pack` that sets a single element of an array from an
   arbitrary Python object.

And in the first part, we need special paths for "object" anyway.  I
can live with a "special case", if it is confined to `PyArray_Pack`.

There are always weird things, e.g. Quantities does *never* want to be
unpacked right now even though it is a subclass so we already have
"tricky stuff" that really means we should channel all through "one
right way", no matter how that way actually looks like.

Cheers,

Sebastian


> 
> Matti
> 
> 
> ___
> 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: sebast...@sipsolutions.net
> 



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