[Numpy-discussion] FeatureRequest: support for array

Peter Creasey p.e.creasey.00 at googlemail.com
Sat Dec 12 15:37:51 EST 2015


> >
> > from itertools import chain
> > def fromiter_awesome_edition(iterable):
> >     elem = next(iterable)
> >     dtype = whatever_numpy_does_to_infer_dtypes_from_lists(elem)
> >     return np.fromiter(chain([elem], iterable), dtype=dtype)
> >
> > I think this would be a huge win for usability. Always getting tripped up by
> > the dtype requirement. I can submit a PR if people like this pattern.
>
> This isn't the semantics of np.array, though -- np.array will look at
> the whole input and try to find a common dtype, so this can't be the
> implementation for np.array(iter). E.g. try np.array([1, 1.0])
>
> I can see an argument for making the dtype= argument to fromiter
> optional, with a warning in the docs that it will guess based on the
> first element and that you should specify it if you don't want that.
> It seems potentially a bit error prone (in the sense that it might
> make it easier to end up with code that works great when you test it
> but then breaks later when something unexpected happens), but maybe
> the usability outweighs that. I don't use fromiter myself so I don't
> have a strong opinion.

I’m -1 on this, from an occasional user of np.fromiter, also for the
np.fromiter([1, 1.5, 2]) ambiguity reason. Pure python does a great
job of preventing users from hurting themselves with limited precision
arithmetic, however if their application makes them care enough about
speed (to be using numpy) and memory (to be using np.fromiter), then
it can almost always be assumed that the resulting dtype was important
enough to be specified.

P



More information about the NumPy-Discussion mailing list