# How to convert numpy array items to mojo Float?

**URL:** <https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506>\
**Category:** Mojo\
**Tags:** discussion, 24\_6\
**Created:** [January 31, 2025, 7:05pm UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506 "2025-01-31T19:05:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kraudy](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/kraudy/32/185_2.png) [@kraudy](https://forum.modular.com/u/kraudy)\
**Post date:** [January 31, 2025, 7:05pm UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/1 "2025-01-31T19:05:23Z")

</div>

Hi, i’m trying to get each element of a numpy array as a mojo Float.

So far this works  
`var value: Float64 = Xb.item(i, j).to_float64()`

But the linter sugest using `float(obj)` wich i assume is `Float64()`

When i do the conversion like this  
`var value: Float64 = Float64(Xb.item(i, j)) `

I get a conversion error of PythonObject to Float64. Any idea why? Can it be done without using mojo’s tensors?

---

<div class="post-metadata">

**Author:** ![arthur](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/arthur/32/139_2.png) [@arthur](https://forum.modular.com/u/arthur)\
**Post date:** [February 4, 2025, 5:28pm UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/2 "2025-02-04T17:28:42Z")

</div>

What version are you using?

There _is_ a `float()` function, similar to the `int()` function. These were (very recently) deprecated in favor of using the constructors (`Int()`, `Float64()`, etc.)

In 24.6, SIMD didn’t have a constructor that took a `Floatable` value, so you’d have to use `float()` or `to_float64()`. In the latest nightly, you _should_ be able to do `Float64(item)` … If you’re on nightly and that’s not working for you, please open a bug.

---

<div class="post-metadata">

**Author:** ![kraudy](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/kraudy/32/185_2.png) [@kraudy](https://forum.modular.com/u/kraudy)\
**Post date:** [February 4, 2025, 6:38pm UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/3 "2025-02-04T18:38:26Z")

</div>

Hi arthur.

I’m not on the nightly branch, mojo version: mojo 24.6.0.

Guess i’ll try the nigthly branch or wait till the next release.

---

<div class="post-metadata">

**Author:** ![arthur](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/arthur/32/139_2.png) [@arthur](https://forum.modular.com/u/arthur)\
**Post date:** [February 4, 2025, 7:16pm UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/4 "2025-02-04T19:16:25Z")

</div>

Ah, yeah. In that case, you could use `float(Xb.item(i, j)` to quiet the linter until you update.

---

<div class="post-metadata">

**Author:** ![kraudy](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/kraudy/32/185_2.png) [@kraudy](https://forum.modular.com/u/kraudy)\
**Post date:** [February 5, 2025, 12:45am UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/5 "2025-02-05T00:45:02Z")

</div>

Will do, thanks Arthur

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [February 12, 2025, 12:45am UTC](https://forum.modular.com/t/how-to-convert-numpy-array-items-to-mojo-float/506/6 "2025-02-12T00:45:20Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
