@@ -14,7 +14,6 @@ module CategoricalArrays
14
14
using DataAPI
15
15
using Missings
16
16
using Printf
17
- using Requires: @require
18
17
19
18
# JuliaLang/julia#36810
20
19
if VERSION < v " 1.5.2"
@@ -35,66 +34,16 @@ module CategoricalArrays
35
34
36
35
include (" deprecated.jl" )
37
36
38
- function __init__ ()
39
- @require JSON= " 682c06a0-de6a-54ab-a142-c8b1cf79cde6" begin
40
- # JSON of CategoricalValue is JSON of the value it refers to
41
- JSON. lower (x:: CategoricalValue ) = JSON. lower (unwrap (x))
42
- end
43
-
44
- @require RecipesBase= " 3cdcf5f2-1ef4-517c-9805-6587b60abb01" @eval begin
45
- RecipesBase. @recipe function f (:: Type{T} , v:: T ) where T <: CategoricalValue
46
- level_strings = [map (string, levels (v)); missing ]
47
- ticks --> eachindex (level_strings)
48
- v -> ismissing (v) ? length (level_strings) : Int (refcode (v)),
49
- i -> level_strings[Int (i)]
50
- end
51
- end
52
-
53
- @require SentinelArrays= " 91c51154-3ec4-41a3-a24f-3f23e20d615c" begin
54
- copyto! (dest:: CatArrOrSub{<:Any, 1} , src:: SentinelArrays.ChainedVector ) =
55
- copyto! (dest, 1 , src, 1 , length (src))
56
- copyto! (dest:: CatArrOrSub{<:Any, 1} , dstart:: Union{Signed, Unsigned} ,
57
- src:: SentinelArrays.ChainedVector , sstart:: Union{Signed, Unsigned} ,
58
- n:: Union{Signed, Unsigned} ) =
59
- invoke (copyto!, Tuple{AbstractArray, Union{Signed, Unsigned},
60
- SentinelArrays. ChainedVector,
61
- Union{Signed, Unsigned}, Union{Signed, Unsigned}},
62
- dest, dstart, src, sstart, n)
63
- end
64
-
65
- @require StructTypes= " 856f2bd8-1eba-4b0a-8007-ebc267875bd4" begin
66
- # define appropriate handlers for JSON3 interface
67
- StructTypes. StructType (x:: CategoricalValue ) = StructTypes. StructType (unwrap (x))
68
- StructTypes. StructType (:: Type{<:CategoricalValue{T}} ) where {T} = StructTypes. StructType (T)
69
- StructTypes. numbertype (:: Type{<:CategoricalValue{T}} ) where {T <: Number } = T
70
- StructTypes. construct (:: Type{T} , x:: CategoricalValue{T} ) where {T} = T (unwrap (x))
71
-
72
- # JSON3 writing/reading
73
- StructTypes. StructType (:: Type{<:CategoricalVector} ) = StructTypes. ArrayType ()
74
-
75
- StructTypes. construct (:: Type{<:CategoricalArray} , A:: AbstractVector ) =
76
- constructgeneral (A)
77
- StructTypes. construct (:: Type{<:CategoricalArray} , A:: Vector ) =
78
- constructgeneral (A)
79
-
80
- function constructgeneral (A)
81
- if eltype (A) === Any
82
- # unlike `replace`, broadcast narrows the type, which allows us to return small
83
- # union eltypes (e.g. Union{String,Missing})
84
- categorical (ifelse .(A .=== nothing , missing , A))
85
- elseif eltype (A) >: Nothing
86
- categorical (replace (A, nothing => missing ))
87
- else
88
- categorical (A)
89
- end
90
- end
37
+ if ! isdefined (Base, :get_extension )
38
+ using Requires: @require
39
+ end
91
40
92
- StructTypes . construct ( :: Type{<:CategoricalArray{Union{Missing, T}}} ,
93
- A :: AbstractVector ) where {T} =
94
- CategoricalArray {Union{Missing, T}} ( replace (A, nothing => missing ) )
95
- StructTypes . construct ( :: Type{<:CategoricalArray{Union{Missing, T}}} ,
96
- A :: Vector ) where {T} =
97
- CategoricalArray {Union{Missing, T}} ( replace (A, nothing => missing ) )
41
+ @static if ! isdefined (Base, :get_extension )
42
+ function __init__ ()
43
+ @require JSON = " 682c06a0-de6a-54ab-a142-c8b1cf79cde6 " include ( " ../ext/CategoricalArraysJSONExt.jl " )
44
+ @require RecipesBase = " 3cdcf5f2-1ef4-517c-9805-6587b60abb01 " include ( " ../ext/CategoricalArraysRecipesBaseExt.jl " )
45
+ @require SentinelArrays = " 91c51154-3ec4-41a3-a24f-3f23e20d615c " include ( " ../ext/CategoricalArraysSentinelArraysExt.jl " )
46
+ @require StructTypes = " 856f2bd8-1eba-4b0a-8007-ebc267875bd4 " include ( " ../ext/CategoricalArraysStructTypesExt.jl " )
98
47
end
99
48
end
100
49
end
0 commit comments