# Sum of an empty array

**URL:** <https://discourse.julialang.org/t/sum-of-an-empty-array/94130>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 6, 2023, 10:22am UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130 "2023-02-06T10:22:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 6, 2023, 10:22am UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/1 "2023-02-06T10:22:14Z")

</div>

Hello,  
I need to calculate the sum of the values in the column of a DataFrame. But it can also be empty.  
How can I do that?

```julia
sum([])

```

gives an long stack trace…

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [February 6, 2023, 10:26am UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/2 "2023-02-06T10:26:55Z")

</div>

Both of these work

```julia
julia> sum(Float64[])
0.0

julia> sum([], init=0.0)
0.0

```

As long as the column is typed, `sum` should just work:

```julia
julia> df = DataFrame(:x => Float64[])
0×1 DataFrame
 Row │ x
     │ Float64
─────┴─────────

julia> sum(df[!,:x])
0.0

julia> combine(df, :x => sum)
1×1 DataFrame
 Row │ x_sum
     │ Float64
─────┼─────────
   1 │ 0.0

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 6, 2023, 10:41am UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/3 "2023-02-06T10:41:28Z")

</div>

Well, the column type is a union of missing and bool, therefore sum(skipmissing(df.x)) did not work, but sum(skipmissing(df.x), init=0) works… 🙂

---

<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:** [February 6, 2023, 12:05pm UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/4 "2023-02-06T12:05:15Z")

</div>

Are you sure that’s the problem?

```julia
julia> sum(skipmissing([true, missing]))
1

julia> typeof([true, missing])
Vector{Union{Missing, Bool}}

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 6, 2023, 12:20pm UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/5 "2023-02-06T12:20:15Z")

</div>

Yes, that was my problem. Try:

```julia
sum(skipmissing([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:** [February 6, 2023, 1:27pm UTC](https://discourse.julialang.org/t/sum-of-an-empty-array/94130/6 "2023-02-06T13:27:54Z")

</div>

there is a recent PR for [`emptymissing`](https://github.com/JuliaData/Missings.jl/pull/143) in Missings.jl which solves this.

```julia
emptymissing(sum)(skipmissing([missing]))

```

maybe not the most elegant syntax, but it will propagate `missing` in this scenario.

Also, with Julia 1.6 you can use `init` as a keyword argument.

```julia
julia> emptymissing(sum)(skipmissing([missing]))
missing

julia> sum(skipmissing([missing]); init = 0.0)
0.0

```
