# Cannot fix \`nothing\` in summation

**URL:** <https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507>\
**Category:** Data\
**Tags:** dataframesmeta\
**Created:** [September 19, 2025, 8:56am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507 "2025-09-19T08:56:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [September 19, 2025, 8:56am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/1 "2025-09-19T08:56:31Z")

</div>

Hello,

I am trying to perform the following transformation on a `DataFrame`:

```julia
@transform! df_20_P_raw[:data] begin
    # Calculate the sum of the relevant columns, replacing missing values with 0
    :pensions_balance_professional = sum(coalesce.((AsTable(r"^pensions_balance_professional_"))..., 0))
    :pensions_balance_voluntary = sum(coalesce.((AsTable(r"^pensions_balance_voluntary_"))..., 0))
end;

```

I have tried several ways, getting different errors each time, but all related to summing `nothing` and `missing` values. Right no,w the error I am getting is:

```julia-auto
ERROR: MethodError: no method matching +(::Nothing, ::Nothing)
The function `+` exists, but no method is defined for this combination of argument types.

```

Any suggestion about how to overcome it?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 19, 2025, 9:26am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/2 "2025-09-19T09:26:44Z")

</div>

Well, what do you think adding `nothing` or `missing` values should give? It seems you want to replace `missing`s with zero, so you might want to do the same with `nothing`. i.e. `replace(vector, nothing => 0)`

---

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [September 19, 2025, 10:02am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/3 "2025-09-19T10:02:54Z")

</div>

> [@nilshg](#):
>
> replace(vector, nothing =\> 0)

Thanks, but it does not work.  
Prior I have a function that at some point has:

```julia-auto
    println(" Fixing type of numeric variables...")
    for name in current_names
        if !(name in non_num_vars) && !(eltype(selected_df[!, name]) <: Number)
            replace!(selected_df[!, name], nothing => 0.0)
            println(" Replaced 'nothing' with zeros in column '$name'.")

            try
                selected_df[!, name] = coalesce.(tryparse.(Float64, selected_df[!, name]), 0.0)
            catch
                println(" Warning: Could not convert column '$name' to numeric.")
            end
        end
    end

```

I am receiving the substitution messages, but they do not actually substitute.  
I have also tried `selected_df[!, name] .= map(x -> x === nothing ? missing : x, selected_df[!, name])` instead of `replace!`, but it does not work either.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [September 19, 2025, 10:49am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/4 "2025-09-19T10:49:14Z")

</div>

Some variation of this, maybe?

```julia-auto
julia> df=DataFrame(a=[1.0, 2.0, nothing, 4.0, 5.0])
5×1 DataFrame
 Row │ a      
     │ Union…
─────┼────────
   1 │ 1.0
   2 │ 2.0
   3 │
   4 │ 4.0
   5 │ 5.0

julia> df.a = ifelse.(df.a .=== nothing, 0.0, df.a)
5-element Vector{Float64}:
 1.0
 2.0
 0.0
 4.0
 5.0

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 19, 2025, 11:02am UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/5 "2025-09-19T11:02:28Z")

</div>

I think you’ll have to provide an MWE, because this straightforwardly works:

```julia-auto
julia> using DataFrames

julia> df = DataFrame(x = rand(5), y = [1, 2, nothing, 4, 5])
5×2 DataFrame
 Row │ x y
     │ Float64 Union…
─────┼──────────────────
   1 │ 0.545902 1
   2 │ 0.119543 2
   3 │ 0.373656
   4 │ 0.389469 4
   5 │ 0.396225 5

julia> for name ∈ names(df)
           replace!(df[!, name], nothing => 0)
       end

julia> df
5×2 DataFrame
 Row │ x y
     │ Float64 Union…
─────┼──────────────────
   1 │ 0.545902 1
   2 │ 0.119543 2
   3 │ 0.373656 0
   4 │ 0.389469 4
   5 │ 0.396225 5

```

---

<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:** [September 19, 2025, 1:48pm UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/6 "2025-09-19T13:48:31Z")

</div>

What about `skipmissing`

```julia-auto
julia> df = DataFrame(a = [1, 2, missing], b = [4, missing, missing])
3×2 DataFrame
 Row │ a b       
     │ Int64? Int64?  
─────┼──────────────────
   1 │ 1 4
   2 │ 2 missing 
   3 │ missing missing 

julia> @rtransform df :z = sum(skipmissing(AsTable([:a, :b]),))
3×3 DataFrame
 Row │ a b z     
     │ Int64? Int64? Int64 
─────┼─────────────────────────
   1 │ 1 4 5
   2 │ 2 missing 2
   3 │ missing missing 0

```

I would guess that you are coming from Stata. Let us know what kind of syntax you think would be easiest to replicate `rowtotal`.

The following helper function might be nice for you

```julia-auto
julia> function rowtotal(x, missingval = 0)
           sum(t -> coalesce(t, missingval), x)
       end;

julia> @rtransform df :z = rowtotal(AsTable([:a, :b]))
3×3 DataFrame
 Row │ a b z     
     │ Int64? Int64? Int64 
─────┼─────────────────────────
   1 │ 1 4 5
   2 │ 2 missing 2
   3 │ missing missing 0

```

---

<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:** [September 19, 2025, 1:57pm UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/7 "2025-09-19T13:57:55Z")

</div>

And it seems like your real issue is that `nothing` is being emitted from `tryparse` on failures, rather than `missing`. This is indeed an annoyance. I brought this issue up [here](https://github.com/JuliaLang/julia/issues/37162) and it was rejected. The recommendation is to use `something(tryparse(...), missing)`, i.e.

```julia-auto
julia> nums = ["5.00", "6.00%"]
2-element Vector{String}:
 "5.00"
 "6.00%"

julia> map(nums) do n
           tryparse(Float64, n)
       end
2-element Vector{Union{Nothing, Float64}}:
 5.0
  nothing

julia> map(nums) do n
           something(tryparse(Float64, n), missing)
       end
2-element Vector{Union{Missing, Float64}}:
 5.0
  missing

```

---

<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:** [September 19, 2025, 2:04pm UTC](https://discourse.julialang.org/t/cannot-fix-nothing-in-summation/132507/8 "2025-09-19T14:04:40Z")

</div>

Another thing, is that you might be missing some nice utilities in DataFrames.jl for highlighting numeric columns.

```julia-auto
julia> df = DataFrame(a = ["dog", "cat"], b = [1, 2], c = [5.0, 6.0])
2×3 DataFrame
 Row │ a b c       
     │ String Int64 Float64 
─────┼────────────────────────
   1 │ dog 1 5.0
   2 │ cat 2 6.0

julia> names(df, Number)
2-element Vector{String}:
 "b"
 "c"

```
