Skip to content

Commit

Permalink
Wrote test cases for skipmissing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBillson committed Aug 27, 2023
1 parent 1048c58 commit 2271445
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/skipmissing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function _missing(x::AbstractFloat, itr)
if isnothing(missingval(itr))
return false
elseif isnan(missingval(itr))
return ismissing(x) || isnan(x)
return isnan(x)
else
return ismissing(x) || x == missingval(itr)
return x == missingval(itr)
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ end
end

@testset "skipmissing uses missingval" begin
# Test missingval=NaN
raster = Raster([NaN 1.0; 2.0 NaN], (X, Y); missingval=NaN)
@test collect(skipmissing(raster)) == [2.0, 1.0]
@test collect(keys(skipmissing(raster))) == [CartesianIndex(2, 1), CartesianIndex(1, 2)]
@test collect(eachindex(skipmissing(raster))) == [2, 3]
@test_throws MissingException skipmissing(raster)[1]
@test skipmissing(raster)[2] == 2.0

# It skips actual missing values as well
mraster = Raster([NaN 1.0; missing NaN], (X, Y); missingval=NaN)
@test collect(skipmissing(mraster)) == [1.0]
Expand All @@ -106,6 +108,13 @@ end
@test skipmissing(mraster)[3] == 1.0
@test_throws MissingException skipmissing(mraster)[2]

# Test missingval=nothing
fraster = Raster([NaN 1.0; 2.0 NaN], (X, Y); missingval=nothing)
mraster = Raster([NaN 1.0; missing NaN], (X, Y); missingval=nothing)
@test length(collect(skipmissing(fraster))) == 4 # Keeps NaN
@test length(collect(skipmissing(mraster))) == 3 # Drops missing

# Confirm that missingvals are removed by value, even when types don't match
r = Raster(ones(Int16, 8, 8), (X,Y); missingval = Int16(-9999))
r[1:4, 1:4] .= -9999
r = float.(r)
Expand Down

0 comments on commit 2271445

Please sign in to comment.