# How to convert the String15 datatype in DataFrames.jl to Float64

**URL:** <https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907>\
**Category:** General Usage\
**Tags:** type, dataframes, convert\
**Created:** [March 11, 2023, 8:01am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907 "2023-03-11T08:01:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 11, 2023, 8:01am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/1 "2023-03-11T08:01:34Z")

</div>

I have a DataFrame with 2 columns in type String15 and String31, respectively.  
Now I want to convert element type from 2 columns to Float64 but I can’t found any document or example in DataFrames.jl’s document.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2a04c4aecc97b9131fe7deba329d7b86cf3aa50.png)

My first try:  
`removed_missing_df[:, "Area"] = map(Float64, removed_missing_df[:, "Area"])`

return error:

```julia
MethodError: no method matching Float64(::InlineStrings.String15)

Closest candidates are:

(::Type{T})(!Matched::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50

(::Type{T})(!Matched::Base.TwicePrecision) where T<:Number at twiceprecision.jl:266

(::Type{T})(!Matched::Complex) where T<:Real at complex.jl:44

...

    iterate@generator.jl:47[inlined]
    _collect@array.jl:807[inlined]
    collect_similar(::Vector{InlineStrings.String15}, ::Base.Generator{Vector{InlineStrings.String15}, Type{Float64}})@array.jl:716
    map(::Type, ::Vector{InlineStrings.String15})@abstractarray.jl:2933
    top-level scope@Local: 1[inlined]

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 11, 2023, 9:25am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/2 "2023-03-11T09:25:07Z")

</div>

Look into the `transform` function and the transformation `x -> parse(Float64, x)`.

Also, if you are using CSV.jl to read in the data, look into specifying the type when you call the `read` function.

---

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 11, 2023, 11:06am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/3 "2023-03-11T11:06:09Z")

</div>

can you give me a sample code for `transform` function?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 11, 2023, 11:56am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/4 "2023-03-11T11:56:31Z")

</div>

Say you’ve downloaded the file [StarWars.csv](https://raw.githubusercontent.com/jump-dev/JuMP.jl/master/docs/src/tutorials/getting_started/data/StarWars.csv).

Then reading it in using `CSV.jl`:

```julia
df = CSV.read("StarWars.csv", DataFrame)

```

we get the `:Weight` column has type `String7` since Jabba’s weight is given as “NA”.  
Updating from [here](https://starwars.fandom.com/wiki/Jabba_Desilijic_Tiure), we might do

```julia
df[15, :Weight] = "1358" 

```

but now we want to interpret this `:Weight` column as containing `Float64` data.  
So we do:

```julia
transform!(df, :Weight => ( x -> parse.(Float64,x) ) => :Weight)

```

do convert the column. (See [First Steps with DataFrames.jl](https://dataframes.juliadata.org/stable/man/basics/#First-Steps-with-DataFrames.jl) for more on this.)

If you give the types at the time you do `CSV.read`, the `String7` type becomes a `Union{Missing, Float64}` instead:

```julia
df = CSV.read("StarWars.csv", DataFrame; types=Dict(:Weight => Float64))

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 11, 2023, 12:14pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/5 "2023-03-11T12:14:24Z")

</div>

DataFramesMeta.jl can simplify some of this syntax; check out the examples for [Propagating missing values with `@passmissing`](https://juliadata.github.io/DataFramesMeta.jl/stable/#Propagating-missing-values-with-@passmissing)

---

<div class="post-metadata">

**Author:** ![gustafsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustafsson/32/3761_2.png) [@gustafsson](https://discourse.julialang.org/u/gustafsson)\
**Post date:** [March 11, 2023, 4:37pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/6 "2023-03-11T16:37:50Z")

</div>

> [@hungpham3112](#):
>
> `removed_missing_df[:, "Area"] = map(Float64, removed_missing_df[:, "Area"])`

Seems you’re looking for an element-wise parse:

`removed_missing_df[:, "Area"] = parse.(Float64, removed_missing_df[:, "Area"])`

* * *

I guess (like @jd-foster) that the issue stems from missing values in the input. If you’re using CSV it’s also possible to tell `CSV.read` directly which token to interpret as a missing value:

```julia
julia> df = CSV.read("StarWars.csv", DataFrame; missingstring="NA")
julia> eltype(df.Weight)
Union{Missing, Float64}

```

---

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 12, 2023, 2:57am UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/7 "2023-03-12T02:57:53Z")

</div>

> [@gustafsson](#):
>
> removed\_missing\_df[:, “Area”] = parse.(Float64, removed\_missing\_df[:, “Area”])

The `:` syntax seems not working in this situation, I changed `:` =\> `!`  
`remove_missing_df[!, "Area"] = parse.(Float64, remove_missing_df[!, "Area"])`

Now I have a different question. Why `!` works while `:` throws error

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/7/c7a8a0b77c1327cf8d39f43689f1fb9dc87cb74f.png)

The error messages in here also weird, it should be  
`Cannot `convert` an object of type InlineStrings.String15 to an object of type Float64`

Is it a bug?

---

<div class="post-metadata">

**Author:** ![gustafsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustafsson/32/3761_2.png) [@gustafsson](https://discourse.julialang.org/u/gustafsson)\
**Post date:** [March 12, 2023, 3:44pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/8 "2023-03-12T15:44:42Z")

</div>

Ups, my bad. One should use ! here.

DataFrames.jl doesn’t always follow Julia rules and conventions, neither for assignment, indexing, type promotion, concatenation nor broadcasting. Instead it has its own set of rules: [Indexing · DataFrames.jl](https://dataframes.juliadata.org/stable/lib/indexing/)

As to your question “Why?”. No particular reason afaik.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 12, 2023, 5:48pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/9 "2023-03-12T17:48:50Z")

</div>

I don’t think so. Considere this:

```julia
julia> vs=["1.2","3.4","2.0"]
3-element Vector{String}:
 "1.2"
 "3.4"
 "2.0"

julia> df=DataFrame(;vs)
3×1 DataFrame
 Row │ vs     
     │ String
─────┼────────
   1 │ 1.2
   2 │ 3.4
   3 │ 2.0

julia> eltype(df.vs)
String

julia> df.vs[1]=99.9
ERROR: MethodError: Cannot `convert` an object of type Float64 to an object of type String

julia> df.vs[1]=parse(Float64,"99.9")
ERROR: MethodError: Cannot `convert` an object of type Float64 to an object of type String
Closest candidates are:
  

```

and this

```julia
julia> vs[1]=9.99
ERROR: MethodError: Cannot `convert` an object of type Float64 to an object of type String
Closest candidates are:

```

then the “responsible” is julia non DataFrames

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 12, 2023, 5:52pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907/10 "2023-03-12T17:52:14Z")

</div>

you can use parse inside the transform function

```julia
julia> df
3×1 DataFrame
 Row │ vs     
     │ String
─────┼────────
   1 │ 1.2
   2 │ 3.4
   3 │ 2.0

julia> transform(df, :vs=>ByRow(x->parse(Float64, x))=>:fls)      
3×2 DataFrame
 Row │ vs fls     
     │ String Float64
─────┼─────────────────
   1 │ 1.2 1.2
   2 │ 3.4 3.4
   3 │ 2.0 2.0

```
