Casting scalars

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Casting scalars

einstein.edison
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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

Stuart Reynolds
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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

Nathan Goldbaum
In reply to this post by einstein.edison
In [1]: import numpy as np

In [2]: np.float64(12)
Out[2]: 12.0

In [3]: np.float64(12).dtype
Out[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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

einstein.edison
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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

Nathan Goldbaum
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).dtype
Out[5]: dtype('float64')

In [6]: np.float is float
Out[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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

Warren Weckesser-2
In reply to this post by einstein.edison


On Thu, May 10, 2018 at 10:53 PM, Hameer Abbasi <[hidden email]> 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 <[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
Reply | Threaded
Open this post in threaded view
|

Re: Casting scalars

einstein.edison
This is exactly what I needed! Thanks! On 11/05/2018 at 08:20, Warren
wrote: On Thu, May 10, 2018 at 10:53 PM, Hameer Abbasi
<[hidden email]> 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
<[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