[Numpy-discussion] deprecating float(x) for ndim > 0

Nico Schlömer nico.schloemer at gmail.com
Thu Sep 16 02:31:54 EDT 2021


> I was playing with this though and was a little surprised to find
> NumPy allows things like this:
>
> >>> a = np.array([1, 2, 3])
> >>> a[:] = np.array([[[5, 6, 7]]])
> >>> a
> array([5, 6, 7])

Thanks Aaron for this example! I hadn't seen this before, and indeed
the suggested PR doesn't intercept this case. Perhaps that's something
we should consider deprecating as well. I'll add a comment to the PR;
let's take it from there.

Cheers,
Nico

On Thu, Sep 16, 2021 at 2:00 AM Aaron Meurer <asmeurer at gmail.com> wrote:
>
> Presumably this also changes int(), bool(), and complex() in the same way.
>
> The array API standard (and numpy.array_api) only requires float(),
> bool(), and int() (and soon complex()) for dimension 0 arrays (the
> standard does not have scalars), in part because of this NumPy issue
> https://data-apis.org/array-api/latest/API_specification/array_object.html#float-self.
>
> On Wed, Sep 15, 2021 at 6:18 AM Nico Schlömer <nico.schloemer at gmail.com> wrote:
> >
> > Hi everyone,
> >
> > This is seeking input on PR [1] which I've worked on with @eric-wieser
> > and @seberg. It deprecates
> > ```
> > float(x)
> > ```
> > if `x` is an array of ndim > 0. (It works with all arrays of size 1
> > right now.) This aligns the behavior of float() on ndarrays with
> > float() on lists which already fails today. It also deprecates the
> > implicit conversion to float in assignment expressions like
> > ```
> > a = np.array([1, 2, 3])
> > a[0] = [5]  # deprecated, should be a[0] = 5
>
> This already gives a ValueError in NumPy 1.21.1. Do you mean a[0] =
> np.array([5]) is deprecated?
>
> I was playing with this though and was a little surprised to find
> NumPy allows things like this:
>
> >>> a = np.array([1, 2, 3])
> >>> a[:] = np.array([[[5, 6, 7]]])
> >>> a
> array([5, 6, 7])
>
> Array assignment allows some sort of reverse broadcasting? Given this
> behavior, it seems to me that a[0] = np.array([5]) actually should
> work. Or is the idea that this entire behavior would be deprecated?
>
> Aaron Meurer
>
> > ```
> > In general, the PR makes numpy a tad bit stricter on how it treats
> > scalars vs. single-item arrays.
> >
> > The change also prevents the #1 wrong usage of float(), namely for
> > extracting the scalar value from an array. One should rather use
> > `x[0]` or `x.item()` to that which doesn't convert the value to a
> > Python float.
> >
> > To estimate the impact of the PR, I looked at major numpy dependents
> > like matplotlib, scipy, pandas etc., and of course numpy itself.
> > Except scipy, all projects were virtually clean to start with. Scipy
> > needed some changes for all tests to pass without warning, and all of
> > the changes were improvements. In particular, the deprecation
> > motivates users to use actual scalars when scalars are needed, e.g.,
> > in the case of scipy, as the return value of a goal functional.
> >
> > It'd be great if you could try the branch against your own project and
> > let us know (here or in the PR) about and problems that you might
> > have.
> >
> > Thanks!
> > Nico
> >
> > [1] https://github.com/numpy/numpy/pull/10615
> > [2] https://github.com/numpy/numpy/issues/10404
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at python.org
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list