# Is this a bug of the Julia function "unique"?

**URL:** https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141
**Category:** General Usage
**Tags:** question
**Created:** [October 21, 2021, 10:41am UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141 "2021-10-21T10:41:38Z")
**Posts on this page:** 12
**Page:** 2

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 21, 2021, 1:49pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/21 "2021-10-21T13:49:03Z")

</div>

Doesn’t grow with the PR so it’s probably a manifestation of the same issue. Note that `B = unique(A, dims=1)` also grows on master as shown by the OP.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 21, 2021, 1:50pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/22 "2021-10-21T13:50:00Z")

</div>

Nice! I missed the 4 vs 3 rows in the OP and just thought it didn’t reduce dimensionality at all.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 21, 2021, 1:52pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/23 "2021-10-21T13:52:46Z")

</div>

> [@leon](#):
>
> I’m processing some oceanographic data right now. When people do a CTD cast at a particular sampling station, there is a Cast number associated with it. Sometimes, people leave it blank when there is only a cast.

You mean they made a cast, but took no measurements? In that case, you’d use `missing`, since a value would exist but you don’t have it. The resulting container would have mixed type, like `Union{Float64, Missing}` for example if all other data is `Float64`.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [October 21, 2021, 1:55pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/24 "2021-10-21T13:55:52Z")

</div>

So Julia behaves sometimes IEEE 754 conformant and other times not? To put it mildly, I’m not very pleased.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [October 21, 2021, 1:58pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/25 "2021-10-21T13:58:14Z")

</div>

Different functions have different semantics. [`==`](https://docs.julialang.org/en/v1/base/math/#Base.:==) strictly follows IEEE 754 semantics, [`isequal`](https://docs.julialang.org/en/v1/base/base/#Base.isequal) doesn’t and it’s well documented what it does.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [October 21, 2021, 1:58pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/26 "2021-10-21T13:58:36Z")

</div>

all documented here [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/)

> Julia provides additional functions to test numbers for special values, which can be useful in situations like hash key comparisons

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [October 21, 2021, 2:21pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/27 "2021-10-21T14:21:13Z")

</div>

@kristoffer.carlsson, is there a policy of backporting, or not, bugfixes, such as that PR of yours? If not, is it too late for 1.7, because we would really want bugs fixed (in the latest version), if this is for sure correct.

I tested your code (or I think its equivalent):

```julia
julia> A = [1 NaN 3; 1 NaN 3; 1 2 3];

julia> B = nunique(A, dims=2)
3×3 Matrix{Float64}:
 1.0 NaN 3.0
 1.0 NaN 3.0
 1.0 2.0 3.0

julia> B = nunique(A, dims=1)
2×3 Matrix{Float64}:
 1.0 NaN 3.0
 1.0 2.0 3.0

```

Your change also got this working (correctly?):

```julia
julia> B = nunique(C, dims=2)
3×3 Matrix{Union{Missing, Int64}}:
 1 missing 3
 1 missing 3
 1 2 3

```

Before:

```julia
julia> B = unique(C, dims=2)
ERROR: TypeError: non-boolean (Missing) used in boolean context

```

Looking at your code (or actually the full function at Github, and copy/pasting from there), I get annoying extra letters… not showing if I quote the code, so I don’t do that:

@generated function \_unique\_dims(A::AbstractArray{T,N}, dim::Integer) where {T,N}  
quote  
[…]  
k = i\_d  
j\_d = uniquerow[k]  
￼ else  
￼ j\_d = i\_d

there’s also other strangeness in the REPL (scrolling back), as if I copy/pasted a lot of times, but I didn’t… so far no crash.

> [@woclass](#):
>
> ```julia
> julia> unique(A)
> 3-element Vector{Float64}:
> 1.0
> NaN
> 3.0
> 
> ```

That part seems right (so not funny?!).

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [October 21, 2021, 2:22pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/28 "2021-10-21T14:22:50Z")

</div>

> [@Palli](#):
>
> is there a policy of backporting, or not, bugfixes, such as that PR of yours?

Have you noticed the `backport 1.6` and `backport 1.7` labels that [had been already added to the PR](https://github.com/JuliaLang/julia/pull/42737#event-5498376774)? 😉

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 21, 2021, 2:51pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/29 "2021-10-21T14:51:10Z")

</div>

The made a cast and did the measurement, but forgot to write down the cast number.

The thing is that a lot of the time, such information is pulled from other sources, e.g., Matlab, etc., which uses NaN.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [October 21, 2021, 3:07pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/30 "2021-10-21T15:07:00Z")

</div>

> [@leon](#):
>
> The thing is that a lot of the time, such information is pulled from other sources, e.g., Matlab, etc., which uses NaN.

Can you sanitise the input and convert `NaN` to `missing`? Or are there some `NaN`s that aren’t missing values? Following what Matlab does because they can’t do better doesn’t seem a good argument.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 21, 2021, 8:01pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/31 "2021-10-21T20:01:44Z")

</div>

That’s what I’m going to do. Many thanks!

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 21, 2021, 8:49pm UTC](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141/32 "2021-10-21T20:49:14Z")

</div>

> [@goerch](#):
>
> I’m not very pleased.

well, it’s a bug, and is being fixed.

[Previous page](https://discourse.julialang.org/t/is-this-a-bug-of-the-julia-function-unique/70141.md?page=1)
