# Replacing missing values in dataframe-convert-type-union-float64-is-ambiguous

**URL:** https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [December 20, 2020, 7:38am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119 "2020-12-20T07:38:13Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![new\_julia\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@new\_julia\_enthusiast](https://discourse.julialang.org/u/new_julia_enthusiast)
#### Post date: [December 20, 2020, 7:38am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/1 "2020-12-20T07:38:13Z")

</div>

Hi, I am a new Julia user and I am having difficulty imputing missing values in my dataframe. The replacement value is dependent on the position of the column and other values in that row. Coming from a Java background, I adopted loops. i.e.,

```julia
for row_counter in 1:size(df,1)
        for column_counter in 1:size(df,2)
            if ismissing(df[row_counter,column_counter])
                next_val = get_next_non_missing_val(df[row_counter,:],column_counter) # get the next non missing value for that row
                if column_counter==0
                    if next_val==-1 # custom logic
                        next_val = 100
                    end
                    df[row_counter,column_counter]=next_val
                elseif column_counter==size(df,2)
                    df[row_counter,column_counter]=df[row_counter,column_counter-1]
                    
                else 
                    if next_val==-1 
                        next_val=df[row_counter,column_counter-1]
                    end
                    
                    df[row_counter,column_counter] = (df[row_counter,column_counter-1]+next_val)/2
                    println(df[row_counter,column_counter] )
                end
            end
        end
    end

```

However, I keep getting the following error:

```julia
ERROR: LoadError: MethodError: convert(::Type{Union{}}, ::Float64) is ambiguous. Candidates:
  convert(::Type{Union{}}, x) in Base at essentials.jl:169
  convert(::Type{T}, x::Number) where T<:Number in Base at number.jl:7
  convert(::Type{T}, arg) where T<:VecElement in Base at baseext.jl:8
  convert(::Type{T}, x::Number) where T<:AbstractChar in Base at char.jl:179
Possible fix, define
  convert(::Type{Union{}}, ::Number)

```

I believe this is due the fact that as my dataframe has missing values, its considered is considered as Union, but I don’t know how to resolve this issue. Can someone please help me and also advise if the methodology I adopted to impute the missing values is the efficient?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [December 20, 2020, 8:12am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/2 "2020-12-20T08:12:37Z")

</div>

You cut off the useful part of the stack trace, we don’t know where this error happened in your code either so it’s a bit hard to come up with ideas. On first glance it doesn’t seem to me that it’s in the code you displayed, maybe in a function you’re calling?

---

<div class="post-metadata">

### Author: ![new\_julia\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@new\_julia\_enthusiast](https://discourse.julialang.org/u/new_julia_enthusiast)
#### Post date: [December 20, 2020, 8:18am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/3 "2020-12-20T08:18:59Z")

</div>

> [@new\_julia\_enthusiast](#):
>
> `LoadError: MethodError: convert(::Type{Union{}}, ::Float64) is ambiguous. Candidates`

Thanks for the reply. Sorry for the incomplete stacktrace.  
The error is thrown in the line:  
`df[row_counter,column_counter] = (df[row_counter,column_counter-1]+next_val)/2`

This is the rest of the stacktrace:

```julia
Stacktrace:
 [1] convert(::Type{Missing}, ::Float64) at ./missing.jl:69
 [2] setindex!(::Array{Missing,1}, ::Float64, ::Int64) at ./array.jl:847
 [3] insert_single_entry!(::DataFrame, ::Float64, ::Int64, ::Int64) at /home/bumblebee/.julia/packages/DataFrames/yqToF/src/dataframe/dataframe.jl:520
 [4] setindex!(::DataFrame, ::Float64, ::Int64, ::Int64) at /home/bumblebee/.julia/packages/DataFrames/yqToF/src/dataframe/dataframe.jl:560
 [5] handle_missing_values(::DataFrame) at parser.jl:103

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [December 20, 2020, 8:24am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/4 "2020-12-20T08:24:00Z")

</div>

Aha so it appears that one of your columns contains only missing values, therefore it’s typed Array{Missing,1} and you can’t put a Float into it because floats can’t be converted to Missings.

You can convert the column to eltype Union{Float64,Missing} first and then it will work.

---

<div class="post-metadata">

### Author: ![new\_julia\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@new\_julia\_enthusiast](https://discourse.julialang.org/u/new_julia_enthusiast)
#### Post date: [December 20, 2020, 11:56pm UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/5 "2020-12-20T23:56:04Z")

</div>

Thanks!  
I have added the following before the for loop:

```julia
for name in names(df)
        if eltype(df[!,Symbol(name)])==Missing 
        df[!,Symbol(name)]=convert(Vector{Union{Float64,Missing}}, df[!,Symbol(name)])
        end
    end

```

It works, though not sure if this is the best approach. Any suggestions?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 21, 2020, 12:51am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/7 "2020-12-21T00:51:10Z")

</div>

1. You don’t have to add `Symbol`. DataFrames takes strings with indexing now
2. You can do `Vector{Union{Float64, Missing}}(x)` instead of `convert`
3. If you are reading data from a CSV you can specify the eltypes of certain columns on import, which is probably the most elegant solution.

---

<div class="post-metadata">

### Author: ![new\_julia\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@new\_julia\_enthusiast](https://discourse.julialang.org/u/new_julia_enthusiast)
#### Post date: [December 21, 2020, 4:19am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-dataframe-convert-type-union-float64-is-ambiguous/52119/8 "2020-12-21T04:19:59Z")

</div>

Thanks all for the help!
