Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

dataframes.jl - How to convert Vector{Decimal} to Float64 in Julia?

I am just starting Julia and doing some column manipulation. However, I found myself into a column type called Vector{Decimal}. I'd like to convert it into something more common like Float64.

I tried to use the convert(Float64, df.difference), but I got this foloowing error:

ERROR: MethodError: Cannot `convert` an object of type Vector{Decimals.Decimal} to an object of type Float64

What's the best way to handle this type of column? Here is my dataframe:

julia> df1[1:5, :]
5×3 DataFrame
 Row │ base_msrp  sales_amount  difference
     │ Decimal…?  Float64?      Decimal…
─────┼─────────────────────────────────────
   1 │    599.99       479.992     119.998
   2 │    599.99       599.99       -0.00
   3 │    599.99       479.992     119.998
   4 │    599.99       539.991      59.999
   5 │    599.99       539.991      59.999
question from:https://stackoverflow.com/questions/65644599/how-to-convert-vectordecimal-to-float64-in-julia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just do:

Float64.(df.difference)

For an example:

julia> using Decimals

julia> vals = Decimal.(rand(3))
3-element Vector{Decimal}:
 Decimal(0, 8265655272182808, -16)
 Decimal(0, 8864515364687842, -16)
 Decimal(0, 7020504368500311, -16)

julia> Float64.(vals)
3-element Vector{Float64}:
 0.8265655272182808
 0.8864515364687842
 0.7020504368500311

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...