On Thu, May 10, 2018 at 10:53 PM, Hameer Abbasi <einstein.edi...@gmail.com>
wrote:

> Yes, that I know. I meant given a dtype string such as 'uint8' or a
> dtype object. I know I can possibly do np.array(scalar,
> dtype=dtype)[()] but I was looking for a less hacky method.



Apparently the `dtype` object has the attribute `type` that creates objects
of that dtype.

For example,

In [30]: a
Out[30]: array([ 1.,  2.,  3.])

In [31]: dt = a.dtype

In [32]: dt
Out[32]: dtype('float64')

In [33]: x = dt.type(8675309)  # Convert the scalar to a's dtype.

In [34]: x
Out[34]: 8675309.0

In [35]: type(x)
Out[35]: numpy.float64


Warren




> On
> 11/05/2018 at 07:50, Stuart wrote: np.float(scalar) On Thu, May 10,
> 2018 at 7:49 PM Hameer Abbasi <einstein.edi...@gmail.com> wrote:
> Hello, everyone! I might be missing something and this might be a very
> stupid and redundant question, but is there a way to cast a scalar to
> a given dtype? Hameer _______________________________________________
> NumPy-Discussion mailing list NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

Reply via email to