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

Commit d11cba1

Browse files
committed
Merge pull request #150 from yesimon/convert
Replace array with Base.convert
2 parents 7aabfa0 + 588d04f commit d11cba1

File tree

7 files changed

+691
-587
lines changed

7 files changed

+691
-587
lines changed

src/dataarray.jl

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -328,80 +328,74 @@ end
328328

329329
#' @description
330330
#'
331-
#' Turn a DataArray into an Array. Raises an error if NA's are encountered.
331+
#' Convert a DataArray{T} to an Array{S}. Throws an `NAException`
332+
#' if the input contains `NA` values that prohibit conversion.
332333
#'
333-
#' @param da::DataArray{T} DataArray that will be converted to an Array.
334+
#' @param da::DataArray{T} The DataArray that will be converted.
334335
#'
335-
#' @returns a::Array{T} Array containing values of `da`.
336+
#' @returns a::Array{S} The (possibly type-converted) elements of
337+
#' `da` if none were `NA`.
336338
#'
337339
#' @examples
338340
#'
339-
#' dv = @data [1, 2, 3, 4]
340-
#' v = array(dv)
341+
#' da = @data [1 2; 3 NA]
342+
#' a = convert(Array{Float64}, da)
341343
#'
342-
#' dm = @data [1 2; 3 4]
343-
#' m = array(dm)
344-
function array{T}(da::DataArray{T}) # -> Array{T}
345-
res = Array(T, size(da))
346-
for i in 1:length(da)
347-
if da.na[i]
348-
throw(NAException())
349-
else
350-
res[i] = da.data[i]
351-
end
344+
#' da = @data [1 2; 3 4]
345+
#' a = convert(Array{Float64}, da)
346+
function Base.convert{S, T, N}(::Type{Array{S, N}},
347+
x::DataArray{T, N}) # -> Array{S, N}
348+
if anyna(x)
349+
err = "Cannot convert DataArray with NA's to desired type"
350+
throw(NAException(err))
351+
else
352+
return convert(Array{S, N}, x.data)
352353
end
353-
return res
354354
end
355355

356-
#' @description
357-
#'
358-
#' Turn a DataArray into an Array. Replace any NA's with the value
359-
#' of second argument, `replacement`.
360-
#'
361-
#' @param da::DataArray{T} DataArray that will be converted to an Array.
362-
#' @param replacement::T Value that will replace NA's in `da`.
363-
#'
364-
#' @returns a::Array{T} Array containing values of `da` plus replacements.
365-
#'
366-
#' @examples
367-
#'
368-
#' dv = @data [1, 2, NA, 4]
369-
#' v = array(dv, 3)
370-
#'
371-
#' dm = @data [1 2; NA 4]
372-
#' m = array(dm, 3)
373-
function array{T}(da::DataArray{T}, replacement::T) # -> Array{T}
374-
res = Array(T, size(da))
356+
function Base.convert{S, T, N}(::Type{Array{S}}, da::DataArray{T, N})
357+
return convert(Array{S, N}, da)
358+
end
359+
360+
function Base.convert{T}(::Type{Vector}, dv::DataVector{T})
361+
return convert(Array{T, 1}, dv)
362+
end
363+
364+
function Base.convert{T}(::Type{Matrix}, dm::DataMatrix{T})
365+
return convert(Array{T, 2}, dm)
366+
end
367+
368+
function Base.convert{T, N}(::Type{Array}, da::DataArray{T, N})
369+
return convert(Array{T, N}, da)
370+
end
371+
372+
function Base.convert{S, T, N}(
373+
::Type{Array{S, N}},
374+
da::DataArray{T, N},
375+
replacement::Any
376+
) # -> Array{S, N}
377+
replacementS = convert(S, replacement)
378+
res = Array(S, size(da))
375379
for i in 1:length(da)
376380
if da.na[i]
377-
res[i] = replacement
381+
res[i] = replacementS
378382
else
379383
res[i] = da.data[i]
380384
end
381385
end
382386
return res
383387
end
384388

385-
#' @description
386-
#'
387-
#' Turn a DataArray into an Array. Replace any NA's with the value
388-
#' of second argument, `replacement`.
389-
#'
390-
#' @param da::DataArray{T} DataArray that will be converted to an Array.
391-
#' @param replacement::Any Value that will replace NA's in `da`.
392-
#' Converted to the `eltype`, `T`, of `da`.
393-
#'
394-
#' @returns a::Array{T} Array containing values of `da` plus replacements.
395-
#'
396-
#' @examples
397-
#'
398-
#' dv = @data [1, 2, NA, 4]
399-
#' v = array(dv, 3)
400-
#'
401-
#' dm = @data [1 2; NA 4]
402-
#' m = array(dm, 3)
403-
function array{T}(da::DataArray{T}, replacement::Any) # -> Array{T}
404-
return array(da, convert(T, replacement))
389+
function Base.convert{T}(::Type{Vector}, dv::DataVector{T}, replacement::Any)
390+
return convert(Array{T, 1}, dv, replacement)
391+
end
392+
393+
function Base.convert{T}(::Type{Matrix}, dm::DataMatrix{T}, replacement::Any)
394+
return convert(Array{T, 2}, dm, replacement)
395+
end
396+
397+
function Base.convert{T, N}(::Type{Array}, da::DataArray{T, N}, replacement::Any)
398+
return convert(Array{T, N}, da, replacement)
405399
end
406400

407401
#' @description
@@ -537,37 +531,6 @@ end
537531
# ::Type{T}) = promote_rule(S, T)
538532
# promote_rule{T}(::Type{AbstractDataArray{T}}, ::Type{T}) = T
539533

540-
#' @description
541-
#'
542-
#' Convert a DataArray{T} to an Array{S}. Throws an `NAException`
543-
#' if the input contains `NA` values that prohibit conversion.
544-
#'
545-
#' @param da::DataArray{T} The DataArray that will be converted.
546-
#'
547-
#' @returns a::Array{S} The (possibly type-converted) elements of
548-
#' `da` if none were `NA`.
549-
#'
550-
#' @examples
551-
#'
552-
#' da = @data [1 2; 3 NA]
553-
#' a = convert(Array{Float64}, da)
554-
#'
555-
#' da = @data [1 2; 3 4]
556-
#' a = convert(Array{Float64}, da)
557-
function Base.convert{S, T, N}(::Type{Array{S, N}},
558-
x::DataArray{T, N}) # -> Array{S, N}
559-
if anyna(x)
560-
err = "Cannot convert DataArray with NA's to desired type"
561-
throw(NAException(err))
562-
else
563-
return convert(Array{S, N}, x.data)
564-
end
565-
end
566-
Base.convert{S,T,N}(::Type{Array{S}}, x::DataArray{T,N}) =
567-
convert(Array{S,N}, x)
568-
Base.convert{T,N}(::Type{Array}, x::DataArray{T,N}) =
569-
convert(Array{T,N}, x)
570-
571534
#' @description
572535
#'
573536
#' Convert an Array{T} to a DataArray{S}.

src/deprecated.jl

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,115 @@
22
# when the deprecation is removed
33
import Base.@deprecate
44
@deprecate (/)(x::Union(NAtype,Number),A::AbstractDataArray) x ./ A
5+
6+
#' @description
7+
#'
8+
#' Turn a DataArray into an Array. Raises an error if NA's are encountered.
9+
#'
10+
#' @param da::DataArray{T} DataArray that will be converted to an Array.
11+
#'
12+
#' @returns a::Array{T} Array containing values of `da`.
13+
#'
14+
#' @examples
15+
#'
16+
#' dv = @data [1, 2, 3, 4]
17+
#' v = convert(Vector, dv)
18+
#'
19+
#' dm = @data [1 2; 3 4]
20+
#' m = convert(Matrix, dm)
21+
function array{T}(da::DataArray{T}) # -> Array{T}
22+
Base.depwarn(
23+
"""
24+
array(da::DataArray{T}) is deprecated.
25+
Use convert(Array, da).
26+
""",
27+
:array
28+
)
29+
res = Array(T, size(da))
30+
for i in 1:length(da)
31+
if da.na[i]
32+
throw(NAException())
33+
else
34+
res[i] = da.data[i]
35+
end
36+
end
37+
return res
38+
end
39+
40+
#' @description
41+
#'
42+
#' Turn a DataArray into an Array. Replace any NA's with the value
43+
#' of second argument, `replacement`.
44+
#'
45+
#' @param da::DataArray{T} DataArray that will be converted to an Array.
46+
#' @param replacement::T Value that will replace NA's in `da`.
47+
#'
48+
#' @returns a::Array{T} Array containing values of `da` plus replacements.
49+
#'
50+
#' @examples
51+
#'
52+
#' dv = @data [1, 2, NA, 4]
53+
#' v = convert(Vector, dv, 3)
54+
#'
55+
#' dm = @data [1 2; NA 4]
56+
#' m = convert(Matrix, dm, 3)
57+
function array{T}(da::DataArray{T}, replacement::Any) # -> Array{T}
58+
Base.depwarn(
59+
"""
60+
array(da::DataArray{T}, replacement::Any) is deprecated.
61+
Use convert(Array, da, replacement) instead.
62+
""",
63+
:array
64+
)
65+
res = Array(T, size(da))
66+
replacementT = convert(T, replacement)
67+
for i in 1:length(da)
68+
if da.na[i]
69+
res[i] = replacementT
70+
else
71+
res[i] = da.data[i]
72+
end
73+
end
74+
return res
75+
end
76+
77+
# Turn a PooledDataArray into an Array. Fail on NA
78+
function array{T, R}(da::PooledDataArray{T, R})
79+
Base.depwarn(
80+
"""
81+
array(pda::PooledDataArray{T, R}) is deprecated.
82+
Use convert(Array, pda) instead.
83+
""",
84+
:array
85+
)
86+
n = length(da)
87+
res = Array(T, size(da))
88+
for i in 1:n
89+
if da.refs[i] == zero(R)
90+
throw(NAException())
91+
else
92+
res[i] = da.pool[da.refs[i]]
93+
end
94+
end
95+
return res
96+
end
97+
98+
function array{T, R}(da::PooledDataArray{T, R}, replacement::T)
99+
Base.depwarn(
100+
"""
101+
array(pda::PooledDataArray{T, R}, replacement::T) is deprecated.
102+
Use convert(Array, pda, replacement) instead.
103+
""",
104+
:array
105+
)
106+
n = length(da)
107+
res = Array(T, size(da))
108+
for i in 1:n
109+
if da.refs[i] == zero(R)
110+
res[i] = replacement
111+
else
112+
res[i] = da.pool[da.refs[i]]
113+
end
114+
end
115+
return res
116+
end

src/pooleddataarray.jl

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ end
459459
##
460460
##############################################################################
461461

462-
Base.find(pdv::PooledDataVector{Bool}) = find(array(pdv, false))
462+
Base.find(pdv::PooledDataVector{Bool}) = find(convert(Vector{Bool}, pdv, false))
463463

464464
##############################################################################
465465
##
@@ -763,33 +763,62 @@ Base.convert{T,R<:Integer,N}(::Type{DataArray}, pda::PooledDataArray{T,R,N}) =
763763

764764
pdata(a::AbstractArray) = convert(PooledDataArray, a)
765765

766-
# Turn a PooledDataArray into an Array. Fail on NA
767-
function array{T, R}(da::PooledDataArray{T, R})
768-
n = length(da)
769-
res = Array(T, size(da))
770-
for i in 1:n
771-
if da.refs[i] == zero(R)
766+
function Base.convert{S, T, R, N}(
767+
::Type{Array{S, N}},
768+
pda::PooledDataArray{T, R, N}
769+
)
770+
res = Array(S, size(pda))
771+
for i in 1:length(pda)
772+
if pda.refs[i] == zero(R)
772773
throw(NAException())
773774
else
774-
res[i] = da.pool[da.refs[i]]
775+
res[i] = pda.pool[pda.refs[i]]
775776
end
776777
end
777778
return res
778779
end
779780

780-
function array{T, R}(da::PooledDataArray{T, R}, replacement::T)
781-
n = length(da)
782-
res = Array(T, size(da))
783-
for i in 1:n
784-
if da.refs[i] == zero(R)
785-
res[i] = replacement
781+
function Base.convert{T, R}(::Type{Vector}, pdv::PooledDataVector{T, R})
782+
return convert(Array{T, 1}, pdv)
783+
end
784+
785+
function Base.convert{T, R}(::Type{Matrix}, pdm::PooledDataMatrix{T, R})
786+
return convert(Array{T, 2}, pdm)
787+
end
788+
789+
function Base.convert{T, R, N}(::Type{Array}, pda::PooledDataArray{T, R, N})
790+
return convert(Array{T, N}, pda)
791+
end
792+
793+
function Base.convert{S, T, R, N}(
794+
::Type{Array{S, N}},
795+
pda::PooledDataArray{T, R, N},
796+
replacement::Any
797+
)
798+
res = Array(S, size(pda))
799+
replacementS = convert(S, replacement)
800+
for i in 1:length(pda)
801+
if pda.refs[i] == zero(R)
802+
res[i] = replacementS
786803
else
787-
res[i] = da.pool[da.refs[i]]
804+
res[i] = pda.pool[pda.refs[i]]
788805
end
789806
end
790807
return res
791808
end
792809

810+
function Base.convert{T, R}(::Type{Vector}, pdv::PooledDataVector{T, R}, replacement::Any)
811+
return convert(Array{T, 1}, pdv, replacement)
812+
end
813+
814+
function Base.convert{T, R}(::Type{Matrix}, pdm::PooledDataMatrix{T, R}, replacement::Any)
815+
return convert(Array{T, 2}, pdm, replacement)
816+
end
817+
818+
function Base.convert{T, R, N}(::Type{Array}, pda::PooledDataArray{T, R, N}, replacement::Any)
819+
return convert(Array{T, N}, pda, replacement)
820+
end
821+
793822
function dropna{T}(pdv::PooledDataVector{T})
794823
n = length(pdv)
795824
res = Array(T, n)

0 commit comments

Comments
 (0)