OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Dealing with columns with datatype object

  • Thread starter Thread starter tamtam_
  • Start date Start date
T

tamtam_

Guest
I read in a stored numpy array with dtype object in polars. I want to change column dtype from object to float in the polars dataframe.

Minimal example:

Code:
# generate sample data
data = {"1": ["1.0", "2.0", "3.0", "4.0"],
        "2": [10, 20, 30, 40]}
df = pl.DataFrame(data, schema={'1':pl.Object, '2':pl.Object})

# what I want to do
df.with_columns(pl.col('2').cast(pl.Float64, strict=False))

The last line yields an error:

polars.exceptions.ComputeError: cannot cast 'Object' type

How can I do this?
<p>I read in a stored numpy array with dtype <em>object</em> in polars.
I want to change column dtype from <em>object</em> to <em>float</em> in the polars dataframe.</p>
<p>Minimal example:</p>
<pre class="lang-py prettyprint-override"><code># generate sample data
data = {"1": ["1.0", "2.0", "3.0", "4.0"],
"2": [10, 20, 30, 40]}
df = pl.DataFrame(data, schema={'1':pl.Object, '2':pl.Object})

# what I want to do
df.with_columns(pl.col('2').cast(pl.Float64, strict=False))
</code></pre>
<p>The last line yields an error:</p>
<blockquote>
<p>polars.exceptions.ComputeError: cannot cast 'Object' type</p>
</blockquote>
<p>How can I do this?</p>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top