Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit f31e879

Browse files
committed
Define Data{T} = Union{T,NAtype}
1 parent 604385d commit f31e879

12 files changed

+43
-37
lines changed

src/abstractdataarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
An `N`-dimensional `AbstractArray` whose entries can take on values of type
55
`T` or the value `NA`.
66
"""
7-
abstract type AbstractDataArray{T, N} <: AbstractArray{Union{T, NAtype}, N} end
7+
abstract type AbstractDataArray{T, N} <: AbstractArray{Data{T}, N} end
88

99
"""
1010
AbstractDataVector{T}

src/extras.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ function StatsBase.addcounts!{T,U,W}(cm::Dict{U,W}, x::AbstractDataArray{T}, wv:
2020
end
2121

2222
function StatsBase.countmap{T}(x::AbstractDataArray{T})
23-
addcounts!(Dict{Union{T, NAtype}, Int}(), x)
23+
addcounts!(Dict{Data{T}, Int}(), x)
2424
end
2525

2626
function StatsBase.countmap{T,W}(x::AbstractDataArray{T}, wv::Weights{W})
27-
addcounts!(Dict{Union{T, NAtype}, W}(), x, wv)
27+
addcounts!(Dict{Data{T}, W}(), x, wv)
2828
end
2929

3030
"""

src/natype.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ A value denoting missingness within the domain of any type.
2929
"""
3030
const NA = NAtype()
3131

32+
const Data{T} = Union{T,NAtype}
33+
3234
Base.show(io::IO, x::NAtype) = print(io, "NA")
3335

3436
struct NAException <: Exception
@@ -37,20 +39,21 @@ end
3739
NAException() = NAException("NA found")
3840

3941
# Restrict to Number to avoid infinite recursion
42+
# Might be possible to get rid of these restrictions if the promotion in base gets changed.
4043
## Numbers
41-
Base.promote_rule(::Type{Union{T,NAtype}}, ::Type{Union{S,NAtype}}) where {T<:Number,S<:Number} =
44+
Base.promote_rule(::Type{Data{T}}, ::Type{Data{S}}) where {T<:Number,S<:Number} =
4245
Union{promote_type(T, S),NAtype}
43-
Base.promote_rule(::Type{Union{T,NAtype}}, ::Type{S}) where {T<:Number,S<:Number} =
46+
Base.promote_rule(::Type{Data{T}}, ::Type{S}) where {T<:Number,S<:Number} =
4447
Union{promote_type(T, S),NAtype}
4548
## Dates
46-
Base.promote_rule(::Type{Union{T,NAtype}}, ::Type{Union{S,NAtype}}) where {T<:Dates.AbstractTime,S<:Dates.AbstractTime} =
49+
Base.promote_rule(::Type{Data{T}}, ::Type{Data{S}}) where {T<:Dates.AbstractTime,S<:Dates.AbstractTime} =
4750
Union{promote_type(T, S),NAtype}
48-
Base.promote_rule(::Type{Union{T,NAtype}}, ::Type{S}) where {T<:Dates.AbstractTime,S<:Dates.AbstractTime} =
51+
Base.promote_rule(::Type{Data{T}}, ::Type{S}) where {T<:Dates.AbstractTime,S<:Dates.AbstractTime} =
4952
Union{promote_type(T, S),NAtype}
5053

5154
# Restrict to Number to avoid maching everything
52-
Base.convert(::Type{Union{T,NAtype}}, x::Number) where {T<:Number} = convert(T, x)
53-
Base.convert(::Type{Union{T,NAtype}}, x::Dates.AbstractTime) where {T<:Dates.AbstractTime} = convert(T, x)
55+
Base.convert(::Type{Data{T}}, x::Number) where {T<:Number} = convert(T, x)
56+
Base.convert(::Type{Data{T}}, x::Dates.AbstractTime) where {T<:Dates.AbstractTime} = convert(T, x)
5457

5558
Base.length(x::NAtype) = 1
5659
Base.size(x::NAtype) = ()
@@ -59,9 +62,9 @@ Base.ndims(x::NAtype) = 0
5962
Base.getindex(x::NAtype, i) = i == 1 ? NA : throw(BoundsError())
6063

6164
extractT(::Type{T}) where {T} = T
62-
extractT(::Type{Union{T,NAtype}}) where {T} = T
65+
extractT(::Type{Data{T}}) where {T} = T
6366

64-
Base.zero(::Type{Union{T,NAtype}}) where {T} = zero(T)
67+
Base.zero(::Type{Data{T}}) where {T} = zero(T)
6568

6669
"""
6770
isna(x) -> Bool

src/operators.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,31 +532,31 @@ function (-){TA,TJ<:Number}(J::UniformScaling{TJ},A::DataArray{TA,2})
532532
end
533533

534534
(+)(A::DataArray{Bool,2},J::UniformScaling{Bool}) =
535-
invoke(+, Tuple{AbstractArray{Union{Bool,NAtype},2},UniformScaling{Bool}}, A, J)
535+
invoke(+, Tuple{AbstractArray{Data{Bool},2},UniformScaling{Bool}}, A, J)
536536
(+)(J::UniformScaling{Bool},A::DataArray{Bool,2}) =
537-
invoke(+, Tuple{UniformScaling{Bool},AbstractArray{Union{Bool,NAtype},2}}, J, A)
537+
invoke(+, Tuple{UniformScaling{Bool},AbstractArray{Data{Bool},2}}, J, A)
538538
(-)(A::DataArray{Bool,2},J::UniformScaling{Bool}) =
539-
invoke(-, Tuple{AbstractArray{Union{Bool,NAtype},2},UniformScaling{Bool}}, A, J)
539+
invoke(-, Tuple{AbstractArray{Data{Bool},2},UniformScaling{Bool}}, A, J)
540540
(-)(J::UniformScaling{Bool},A::DataArray{Bool,2}) =
541-
invoke(-, Tuple{UniformScaling{Bool},AbstractArray{Union{Bool,NAtype},2}}, J, A)
541+
invoke(-, Tuple{UniformScaling{Bool},AbstractArray{Data{Bool},2}}, J, A)
542542

543543
(+){TA,TJ}(A::AbstractDataArray{TA,2},J::UniformScaling{TJ}) =
544-
invoke(+, Tuple{AbstractArray{Union{TA,NAtype},2},UniformScaling{TJ}}, A, J)
544+
invoke(+, Tuple{AbstractArray{Data{TA},2},UniformScaling{TJ}}, A, J)
545545
(+){TA}(J::UniformScaling,A::AbstractDataArray{TA,2}) =
546-
invoke(+, Tuple{UniformScaling,AbstractArray{Union{TA,NAtype},2}}, J, A)
546+
invoke(+, Tuple{UniformScaling,AbstractArray{Data{TA},2}}, J, A)
547547
(-){TA,TJ<:Number}(A::AbstractDataArray{TA,2},J::UniformScaling{TJ}) =
548-
invoke(-, Tuple{AbstractArray{Union{TA,NAtype},2},UniformScaling{TJ}}, A, J)
548+
invoke(-, Tuple{AbstractArray{Data{TA},2},UniformScaling{TJ}}, A, J)
549549
(-){TA,TJ<:Number}(J::UniformScaling{TJ},A::AbstractDataArray{TA,2}) =
550-
invoke(-, Tuple{UniformScaling{TJ},AbstractArray{Union{TA,NAtype},2}}, J, A)
550+
invoke(-, Tuple{UniformScaling{TJ},AbstractArray{Data{TA},2}}, J, A)
551551

552552
(+)(A::AbstractDataArray{Bool,2},J::UniformScaling{Bool}) =
553-
invoke(+, Tuple{AbstractArray{Union{Bool,NAtype},2},UniformScaling{Bool}}, A, J)
553+
invoke(+, Tuple{AbstractArray{Data{Bool},2},UniformScaling{Bool}}, A, J)
554554
(+)(J::UniformScaling{Bool},A::AbstractDataArray{Bool,2}) =
555-
invoke(+, Tuple{UniformScaling{Bool},AbstractArray{Union{Bool,NAtype},2}}, J, A)
555+
invoke(+, Tuple{UniformScaling{Bool},AbstractArray{Data{Bool},2}}, J, A)
556556
(-)(A::AbstractDataArray{Bool,2},J::UniformScaling{Bool}) =
557-
invoke(-, Tuple{AbstractArray{Union{Bool,NAtype},2},UniformScaling{Bool}}, A, J)
557+
invoke(-, Tuple{AbstractArray{Data{Bool},2},UniformScaling{Bool}}, A, J)
558558
(-)(J::UniformScaling{Bool},A::AbstractDataArray{Bool,2}) =
559-
invoke(-, Tuple{UniformScaling{Bool},AbstractArray{Union{BoolNAtype},2}}, J, A)
559+
invoke(-, Tuple{UniformScaling{Bool},AbstractArray{Data{Bool},2}}, J, A)
560560

561561
end # if isdefined(Base, :UniformScaling)
562562

src/pooleddataarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ end
106106
PooledDataArray(d::PooledDataArray) = d
107107

108108
# Constructor from array, w/ pool, missingness, and ref type
109-
function PooledDataArray{T,R<:Integer,N}(d::AbstractArray{<:Union{T,NAtype}, N},
109+
function PooledDataArray{T,R<:Integer,N}(d::AbstractArray{<:Data{T}, N},
110110
pool::Vector{T},
111-
m::AbstractArray{<:Union{Bool,NAtype}, N},
111+
m::AbstractArray{<:Data{Bool}, N},
112112
r::Type{R} = DEFAULT_POOLED_REF_TYPE)
113113
if length(pool) > typemax(R)
114114
throw(ArgumentError("Cannot construct a PooledDataVector with type $R with a pool of size $(length(pool))"))

src/reduce.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Base.varm{T}(A::DataArray{T}, m::NAtype; corrected::Bool=true, skipna::Bool=fals
162162
function Base.var(A::DataArray; corrected::Bool=true, mean=nothing, skipna::Bool=false)
163163
mean == 0 ? Base.varm(A, 0; corrected=corrected, skipna=skipna) :
164164
mean == nothing ? varm(A, Base.mean(A; skipna=skipna); corrected=corrected, skipna=skipna) :
165-
isa(mean, Union{Number, NAtype}) ?
165+
isa(mean, Data{Number}) ?
166166
varm(A, mean; corrected=corrected, skipna=skipna) :
167167
throw(ErrorException("Invalid value of mean."))
168168
end

test/constructors.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
@test isequal(dv, convert(DataArray, 1:3))
2626

2727
dv = DataArray(Int, 3)
28-
@test isequal(eltype(dv), Union{Int,NAtype})
28+
@test isequal(eltype(dv), Data{Int})
2929
@test isequal(dv.na, trues(3))
3030

3131
dv = convert(DataArray, zeros(3))
@@ -67,7 +67,7 @@
6767
@test isequal(pdv, convert(PooledDataArray, PooledDataArray([1, 2, 3])))
6868

6969
pdv = PooledDataArray(Int, 3)
70-
@test isequal(eltype(pdv), Union{Int,NAtype})
70+
@test isequal(eltype(pdv), Data{Int})
7171
@test all(isna.(pdv) .== trues(3))
7272

7373
pdv = convert(PooledDataArray, zeros(3))
@@ -106,7 +106,7 @@
106106
@test isequal(dm, convert(DataArray, trues(2, 2)))
107107

108108
dm = DataArray(Int, 2, 2)
109-
@test isequal(eltype(dm), Union{Int,NAtype})
109+
@test isequal(eltype(dm), Data{Int})
110110
@test isequal(dm.na, trues(2, 2))
111111

112112
@test_nowarn convert(DataArray, zeros(2, 2))

test/data.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@
7171
@test size(dvint) == (4,)
7272
@test length(dvint) == 4
7373
@test sum(isna.(dvint)) == 1
74-
@test eltype(dvint) == Union{Int,NAtype}
74+
@test eltype(dvint) == Data{Int}
7575

7676
#test_group("PooledDataVector methods")
7777
@test size(pdvstr) == (7,)
7878
@test length(pdvstr) == 7
7979
@test sum(isna.(pdvstr)) == 1
80-
@test eltype(pdvstr) == Union{String,NAtype}
80+
@test eltype(pdvstr) == Data{String}
8181

8282
#test_group("DataVector operations")
8383
@test isequal(dvint .+ 1, DataArray([2, 3, 4, 5], [false, false, true, false]))

test/dataarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
end
100100

101101
# Inferrability of map (#276)
102-
@test eltype(map(x -> x > 1, @data [1, 2])) == Union{Bool,NAtype}
102+
@test eltype(map(x -> x > 1, @data [1, 2])) == Data{Bool}
103103

104104
@testset "Issue #278" begin
105105
x = @data ones(4)

test/extras.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
d = @data [NA,3,3]
77
w = weights([1.1,2.2,3.3])
8-
cm = Dict{Union{Int, NAtype}, Int}([(NA, 1), (3, 2)])
9-
cmw = Dict{Union{Int, NAtype}, Real}([(NA, 1.1), (3, 5.5)])
8+
cm = Dict{Data{Int}, Int}([(NA, 1), (3, 2)])
9+
cmw = Dict{Data{Int}, Real}([(NA, 1.1), (3, 5.5)])
1010
@test isequal(countmap(d), cm)
1111
@test isequal(countmap(d, w), cmw)
1212

test/nas.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@
6565

6666
@testset "promotion" for (T1, T2) in ((Int, Float64),
6767
(Dates.Minute, Dates.Second))
68-
@test promote_type(T1, Union{T2,NAtype}) == Union{T2,NAtype}
69-
@test promote_type(Union{T1,NAtype}, T2) == Union{T2,NAtype}
70-
@test promote_type(Union{T1,NAtype}, Union{T2,NAtype}) == Union{T2,NAtype}
68+
@eval begin
69+
@test promote_type($T1, Data{$T2}) == Data{$T2}
70+
@test promote_type(Data{$T1}, $T2) == Data{$T2}
71+
@test promote_type(Data{$T1}, Data{$T2}) == Data{$T2}
72+
end
7173
end
7274

7375
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Base.Test
66
using DataArrays
7+
using DataArrays: Data
78

89
my_tests = ["abstractarray.jl",
910
"booleans.jl",

0 commit comments

Comments
 (0)