@@ -6,27 +6,37 @@ const ≅ = isequal
6
6
7
7
@testset " cut($(Union{Int, T}) [...])" for T in (Union{}, Missing)
8
8
x = @inferred cut (Vector {Union{Int, T}} ([2 , 3 , 5 ]), [1 , 3 , 6 ])
9
- @test x == [" [1, 3)" , " [3, 6) " , " [3, 6) " ]
9
+ @test x == [" [1, 3)" , " [3, 6] " , " [3, 6] " ]
10
10
@test isa (x, CategoricalVector{Union{String, T}})
11
11
@test isordered (x)
12
- @test levels (x) == [" [1, 3)" , " [3, 6)" ]
12
+ @test levels (x) == [" [1, 3)" , " [3, 6]" ]
13
+
14
+ @test cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [2 , 5 ], extend= false ) ==
15
+ [" [2, 5]" , " [2, 5]" , " [2, 5]" ]
13
16
14
17
err = @test_throws ArgumentError cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [3 , 6 ])
15
18
@test err. value. msg == " value 2 (at index 1) does not fall inside the breaks: adapt them manually, or pass extend=true or extend=missing"
16
19
17
20
18
- err = @test_throws ArgumentError cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [2 , 5 ])
19
- @test err. value. msg == " value 5 (at index 3) does not fall inside the breaks: adapt them manually, or pass extend=true or extend=missing"
20
-
21
21
if T === Missing
22
22
x = @inferred cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [2 , 5 ], extend= missing )
23
23
else
24
24
x = cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [2 , 5 ], extend= missing )
25
25
end
26
- @test x ≅ [" [2, 5) " , " [2, 5) " , missing ]
26
+ @test x ≅ [" [2, 5] " , " [2, 5] " , " [2, 5] " ]
27
27
@test isa (x, CategoricalVector{Union{String, Missing}})
28
28
@test isordered (x)
29
- @test levels (x) == [" [2, 5)" ]
29
+ @test levels (x) == [" [2, 5]" ]
30
+
31
+ if T === Missing
32
+ x = @inferred cut (Vector {Union{T, Int}} ([2 , 3 , 6 ]), [2 , 5 ], extend= missing )
33
+ else
34
+ x = cut (Vector {Union{T, Int}} ([2 , 3 , 6 ]), [2 , 5 ], extend= missing )
35
+ end
36
+ @test x ≅ [" [2, 5]" , " [2, 5]" , missing ]
37
+ @test isa (x, CategoricalVector{Union{String, Missing}})
38
+ @test isordered (x)
39
+ @test levels (x) == [" [2, 5]" ]
30
40
31
41
x = @inferred cut (Vector {Union{T, Int}} ([2 , 3 , 5 ]), [3 , 6 ], extend= true )
32
42
@test x == [" [2, 3)" , " [3, 6]" , " [3, 6]" ]
@@ -40,10 +50,10 @@ const ≅ = isequal
40
50
@test levels (x) == [" [2, 3)" , " [3, 6]" ]
41
51
42
52
x = @inferred cut (Vector {Union{T, Int}} ([1 , 2 , 4 ]), [1 , 3 , 6 ])
43
- @test x == [" [1, 3)" , " [1, 3)" , " [3, 6) " ]
53
+ @test x == [" [1, 3)" , " [1, 3)" , " [3, 6] " ]
44
54
@test isa (x, CategoricalVector{Union{String, T}})
45
55
@test isordered (x)
46
- @test levels (x) == [" [1, 3)" , " [3, 6) " ]
56
+ @test levels (x) == [" [1, 3)" , " [3, 6] " ]
47
57
48
58
x = @inferred cut (Vector {Union{T, Int}} ([1 , 2 , 4 ]), [3 , 6 ], extend= true )
49
59
@test x == [" [1, 3)" , " [1, 3)" , " [3, 6]" ]
@@ -67,10 +77,10 @@ const ≅ = isequal
67
77
breaks = [18 , 25 , 35 , 60 , 100 ]
68
78
x = @inferred cut (Vector {Union{T, Int}} (ages), breaks)
69
79
@test x == [" [18, 25)" , " [18, 25)" , " [25, 35)" , " [25, 35)" , " [18, 25)" , " [18, 25)" ,
70
- " [35, 60)" , " [25, 35)" , " [60, 100) " , " [35, 60)" , " [35, 60)" , " [25, 35)" ]
80
+ " [35, 60)" , " [25, 35)" , " [60, 100] " , " [35, 60)" , " [35, 60)" , " [25, 35)" ]
71
81
@test isa (x, CategoricalVector{Union{String, T}})
72
82
@test isordered (x)
73
- @test levels (x) == [" [18, 25)" , " [25, 35)" , " [35, 60)" , " [60, 100) " ]
83
+ @test levels (x) == [" [18, 25)" , " [25, 35)" , " [35, 60)" , " [60, 100] " ]
74
84
75
85
breaks = [1 , 6 , 3 ] # Unsorted breaks
76
86
labels = [" b" , " a" ] # Differs from lexical ordering
@@ -83,10 +93,10 @@ const ≅ = isequal
83
93
@test levels (x) == [" b" , " a" ]
84
94
85
95
x = @inferred cut (Matrix {Union{Float64, T}} ([- 1.1 3.0 ; 1.456 10.394 ]), [- 2.134 , 3.0 , 12.5 ])
86
- @test x == [" [-2.134, 3.0)" " [3.0, 12.5) " ; " [-2.134, 3.0)" " [3.0, 12.5) " ]
96
+ @test x == [" [-2.134, 3.0)" " [3.0, 12.5] " ; " [-2.134, 3.0)" " [3.0, 12.5] " ]
87
97
@test isa (x, CategoricalMatrix{Union{String, T}})
88
98
@test isordered (x)
89
- @test levels (x) == [" [-2.134, 3.0)" , " [3.0, 12.5) " ]
99
+ @test levels (x) == [" [-2.134, 3.0)" , " [3.0, 12.5] " ]
90
100
91
101
labels = 0 : 2 : 8
92
102
x = @inferred cut (Vector {Union{T, Int}} (1 : 8 ), 0 : 2 : 10 , labels= labels)
179
189
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 11 ])
180
190
y = cut (1 : 10 , [1 , 5 , 5 , 11 ], allowempty= true )
181
191
@test y == cut (1 : 10 , [1 , 5 , 11 ])
182
- @test levels (y) == [" [1, 5)" , " (5, 5)" , " [5, 11) " ]
192
+ @test levels (y) == [" [1, 5)" , " (5, 5)" , " [5, 11] " ]
183
193
184
194
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 5 , 11 ])
185
195
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 11 ],
@@ -191,29 +201,29 @@ end
191
201
192
202
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 11 ], labels= string .(1 : 3 ))
193
203
y = cut (1 : 10 , [1 , 5 , 5 , 11 ], allowempty= true , labels= string .(1 : 3 ))
194
- @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11) " => " 3" )
204
+ @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11] " => " 3" )
195
205
@test levels (y) == string .(1 : 3 )
196
206
197
207
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 5 , 11 ], labels= string .(1 : 4 ))
198
208
y = cut (1 : 10 , [1 , 5 , 5 , 5 , 11 ], allowempty= true , labels= string .(1 : 4 ))
199
- @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11) " => " 4" )
209
+ @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11] " => " 4" )
200
210
@test levels (y) == string .(1 : 4 )
201
211
202
212
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 5 , 5 , 11 ], labels= string .(1 : 5 ))
203
213
y = cut (1 : 10 , [1 , 5 , 5 , 5 , 5 , 11 ], allowempty= true , labels= string .(1 : 5 ))
204
- @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11) " => " 5" )
214
+ @test y == recode (cut (1 : 10 , [1 , 5 , 11 ]), " [1, 5)" => " 1" , " [5, 11] " => " 5" )
205
215
@test levels (y) == string .(1 : 5 )
206
216
207
217
@test_throws ArgumentError cut (1 : 10 , [1 , 3 , 3 , 5 , 5 , 11 ], labels= string .(1 : 5 ))
208
218
y = cut (1 : 10 , [1 , 3 , 3 , 5 , 5 , 11 ], allowempty= true , labels= string .(1 : 5 ))
209
219
@test y == recode (cut (1 : 10 , [1 , 3 , 5 , 11 ]),
210
- " [1, 3)" => " 1" , " [3, 5)" => " 3" , " [5, 11) " => " 5" )
220
+ " [1, 3)" => " 1" , " [3, 5)" => " 3" , " [5, 11] " => " 5" )
211
221
@test levels (y) == string .(1 : 5 )
212
222
213
223
@test_throws ArgumentError cut (1 : 10 , [1 , 3 , 3 , 3 , 5 , 5 , 5 , 11 ], labels= string .(1 : 7 ))
214
224
y = cut (1 : 10 , [1 , 3 , 3 , 3 , 5 , 5 , 5 , 11 ], allowempty= true , labels= string .(1 : 7 ))
215
225
@test y == recode (cut (1 : 10 , [1 , 3 , 5 , 11 ]),
216
- " [1, 3)" => " 1" , " [3, 5)" => " 4" , " [5, 11) " => " 7" )
226
+ " [1, 3)" => " 1" , " [3, 5)" => " 4" , " [5, 11] " => " 7" )
217
227
@test levels (y) == string .(1 : 7 )
218
228
219
229
@test_throws ArgumentError cut (1 : 10 , [1 , 3 , 5 , 5 , 11 ],
255
265
end
256
266
257
267
@testset " cut with extend=missing" begin
258
- x = @inferred cut ([- 0.0 , 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ], [- 0.0 , 0.0 , 3.0 ],
268
+ x = @inferred cut ([- 0.0 , 0.0 , 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ], [- 0.0 , 0.0 , 3.0 ],
259
269
labels= [- 0.0 , 0.0 ], extend= missing )
260
- @test x ≅ [- 0.0 , 0.0 , 0.0 , 0.0 , missing , missing ]
270
+ @test x ≅ [- 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , missing , missing ]
261
271
@test x isa CategoricalArray{Union{Missing, Float64},1 ,UInt32}
262
272
@test isordered (x)
263
273
@test levels (x) == [- 0.0 , 0.0 ]
0 commit comments