Casting scalars

7 messages
Open this post in threaded view
|

Casting scalars

 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 [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Casting scalars

 np.float(scalar)On Thu, May 10, 2018 at 7:49 PM Hameer Abbasi <[hidden email]> 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 [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Casting scalars

 In reply to this post by Hameer Abbasi In [1]: import numpy as npIn [2]: np.float64(12)Out[2]: 12.0In [3]: np.float64(12).dtypeOut[3]: dtype('float64')On Thu, May 10, 2018 at 9:49 PM Hameer Abbasi <[hidden email]> 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 [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Casting scalars

 In reply to this post by Stuart Reynolds 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. On 11/05/2018 at 07:50, Stuart wrote: np.float(scalar) On Thu, May 10, 2018 at 7:49 PM Hameer Abbasi <[hidden email]> 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 [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion_______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Casting scalars

 In reply to this post by Stuart Reynolds On Thu, May 10, 2018 at 9:51 PM Stuart Reynolds <[hidden email]> wrote:np.float(scalar)This actually isn't right. It's a common misconception, but np.float is an alias to the built-in float type. You probably want np.float_(scalar)In [5]: np.float_(12).dtypeOut[5]: dtype('float64')In [6]: np.float is floatOut[6]: True On Thu, May 10, 2018 at 7:49 PM Hameer Abbasi <[hidden email]> 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 [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion