-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing.jl
121 lines (88 loc) · 2.85 KB
/
missing.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# const RealOrMissing = Union{Missing,Real}
"""
AbstractMissOrRealArray = AbstractArray{<:Union{T, Missing}} where T <: Real
AbstractMissArray = AbstractArray{Union{T, Missing}} where T <: Real
- `AbstractMissArray`: must have missing value
- `AbstractMissOrRealArray` : may have missing value
# Examples
```
f(x::AbstractMissArray) = x
# f([1, 2]) # not work
f([1, 2, missing])
f([missing])
f2(x::AbstractMissOrRealArray) = x
f2([1, 2])
f2([1, 2, missing])
f2([missing])
```
"""
# 可以含有missing、也可以不含有
AbstractMissOrRealArray = AbstractArray{<:Union{T,Missing}} where {T<:Real}
# 必须含有missing
AbstractMissArray = AbstractArray{Union{T,Missing}} where {T<:Real}
function getDataType(x)
T = eltype(x)
typeof(T) == Union ? x.b : x
end
"""
to_missing(x::AbstractArray{T}, replacement=0)
to_missing(x::AbstractMissArray{T}, replacement=0)
to_missing!(x::AbstractMissArray{T}, replacement=0)
convert `replacement` to `missing`
$(TYPEDSIGNATURES)
# Usage
$(METHODLIST)
"""
function to_missing(x::AbstractArray{T}, replacement=0) where {T<:Real}
x2 = Array{Union{Missing,T}}(x) # this is a deepcopy
to_missing!(x2, replacement)
end
function to_missing(x::AbstractMissArray{T}, replacement=0) where {T<:Real}
replace(x, T(replacement) => missing)
end
# not for user
function to_missing!(x::AbstractMissArray{T}, replacement=0) where {T<:Real}
replace!(x, T(replacement) => missing)
end
"""
drop_missing
$(TYPEDSIGNATURES)
"""
function drop_missing(x::AbstractMissArray{T}, replacement=NaN) where {T<:Real}
x2 = replace(x, missing => T(replacement))
Array{T}(x2)
end
drop_missing(x::AbstractArray{T}, replacement=NaN) where {T<:Real} = x
# not for user
function drop_missing!(x::AbstractMissArray{T}, replacement=NaN) where {T<:Real}
replace!(x, missing => T(replacement))
end
# not for user
drop_missing!(x::AbstractArray{T}, replacement=NaN) where {T<:Real} = x
# function replace_value!(x::AbstractMissOrRealArray{T}, old::O, new::N) where {T<:Real, O<:Real, N<:Real}
# replace!(x, T(old) => T(new))
# end
# function replace_value(x::AbstractMissOrRealArray{T}, old::Missing, new::N) where {T<:Real, N<:Real}
# drop_missing!(x, new)
# end
# # unable to replace value
# function replace_value(x::AbstractArray{T}, old::O, new::Missing) where {T<:Real,O<:Real}
# to_missing(x, old)
# end
# function replace_value!(x::AbstractMissArray{T}, old::O, new::Missing) where {T<:Real, O<:Real}
# to_missing!(x, old)
# end
replace_miss = drop_missing
## for `AbstractMissOrRealArray`: should use `===` to compare
# Base.:-, Base.isequal
function Base.isequal(a::AbstractMissOrRealArray, b::AbstractMissOrRealArray)
size(a) != size(b) && return (false)
for i = eachindex(a)
if (a[i] !== b[i])
return (false)
end
end
return (true)
end
export AbstractMissOrRealArray, AbstractMissArray,
to_missing, drop_missing