Skip to content

Commit

Permalink
Merge pull request JuliaLang#193 from bkamins/naconstr
Browse files Browse the repository at this point in the history
Check for NA in DataArray inner constructor
  • Loading branch information
nalimilan committed Jun 3, 2016
2 parents 2642f6c + 7226e40 commit 8184cee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/dataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ type DataArray{T, N} <: AbstractDataArray{T, N}
data::Array{T, N}
na::BitArray{N}

# Ensure data values and missingness metadata match
function DataArray(d::Array{T, N}, m::BitArray{N})
# Ensure data values and missingness metadata match
if size(d) != size(m)
msg = "Data and missingness arrays must be the same size"
throw(ArgumentError(msg))
end
# additionally check that d does not contain NA entries
if eltype(d) === Any
for i in eachindex(d)
if isassigned(d, i) && isna(d, i)
m[i] = true
end
end
elseif eltype(d) <: NAtype
m = trues(m)
end
new(d, m)
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/dataarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module TestDataArray
similar(dm, 2, 2)
similar(dt, 2, 2, 2)

@test isequal(DataArray([NA, NA], [true, true]), DataArray([NA, NA], [false, false]))
@test isequal(DataArray(Any[1, NA], [false, true]), DataArray(Any[1, NA], [false, false]))
@test isequal(DataArray(Any[1, 2], [false, true]), DataArray(Any[1, NA], [false, false]))

x = DataArray([9, 9, 8])
y = DataArray([1, 9, 3, 2, 2])
Expand Down

0 comments on commit 8184cee

Please sign in to comment.