Skip to content

Commit 15509b2

Browse files
authored
Fix Array{<:CategoricalValue} constructors and convert (#427)
When an `Array{<:CategoricalValue}` is requested explicitly, return such an object rather than a `CategoricalArray`. Also fix existing tests which didn't test what they were supposed to.
1 parent 49e200a commit 15509b2

File tree

2 files changed

+125
-14
lines changed

2 files changed

+125
-14
lines changed

src/array.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ convert(::Type{CategoricalArray{T, N}}, A::CategoricalArray{T, N}) where {T, N}
411411
convert(::Type{CategoricalArray{T}}, A::CategoricalArray{T}) where {T} = A
412412
convert(::Type{CategoricalArray}, A::CategoricalArray) = A
413413

414-
convert(::Type{Array{S, N}}, A::CatArrOrSub{T, N}) where {S, T, N} =
415-
collect(S, A)
416414
convert(::Type{Array}, A::CatArrOrSub) = unwrap.(A)
417415
convert(::Type{Vector}, A::CatArrOrSub) = unwrap.(A)
418416
convert(::Type{Matrix}, A::CatArrOrSub) = unwrap.(A)

test/13_arraycommon.jl

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ end
13361336
z = Array(y)
13371337
@test typeof(x) == typeof(z)
13381338
@test z == x
1339-
z = Array(view(x, 1:4))
1339+
z = Array(view(y, 1:4))
13401340
@test typeof(x) == typeof(z)
13411341
@test z == x
13421342

@@ -1345,7 +1345,7 @@ end
13451345
z = Array(y)
13461346
@test typeof(x) == typeof(z)
13471347
@test z x
1348-
z = Array(view(x, 1:4))
1348+
z = Array(view(y, 1:4))
13491349
@test typeof(x) == typeof(z)
13501350
@test z x
13511351

@@ -1354,7 +1354,7 @@ end
13541354
z = Vector(y)
13551355
@test typeof(x) == typeof(z)
13561356
@test z == x
1357-
z = Vector(view(x, 1:4))
1357+
z = Vector(view(y, 1:4))
13581358
@test typeof(x) == typeof(z)
13591359
@test z == x
13601360

@@ -1363,7 +1363,7 @@ end
13631363
z = Vector(y)
13641364
@test typeof(x) == typeof(z)
13651365
@test z x
1366-
z = Vector(view(x, 1:4))
1366+
z = Vector(view(y, 1:4))
13671367
@test typeof(x) == typeof(z)
13681368
@test z x
13691369

@@ -1372,7 +1372,7 @@ end
13721372
z = Matrix(y)
13731373
@test typeof(x) == typeof(z)
13741374
@test z == x
1375-
z = Matrix(view(x, :, 1:4))
1375+
z = Matrix(view(y, :, 1:4))
13761376
@test typeof(x) == typeof(z)
13771377
@test z == x
13781378

@@ -1381,7 +1381,7 @@ end
13811381
z = Matrix(y)
13821382
@test typeof(x) == typeof(z)
13831383
@test z x
1384-
z = Matrix(view(x, :, 1:4))
1384+
z = Matrix(view(y, :, 1:4))
13851385
@test typeof(x) == typeof(z)
13861386
@test z x
13871387
end
@@ -1392,7 +1392,7 @@ end
13921392
z = convert(Array, y)
13931393
@test typeof(x) == typeof(z)
13941394
@test z == x
1395-
z = Array(view(x, 1:4))
1395+
z = convert(Array, view(y, 1:4))
13961396
@test typeof(x) == typeof(z)
13971397
@test z == x
13981398

@@ -1401,7 +1401,7 @@ end
14011401
z = convert(Array, y)
14021402
@test typeof(x) == typeof(z)
14031403
@test z x
1404-
z = Array(view(x, 1:4))
1404+
z = convert(Array, view(y, 1:4))
14051405
@test typeof(x) == typeof(z)
14061406
@test z x
14071407

@@ -1410,7 +1410,7 @@ end
14101410
z = convert(Vector, y)
14111411
@test typeof(x) == typeof(z)
14121412
@test z == x
1413-
z = Vector(view(x, 1:4))
1413+
z = convert(Vector, view(y, 1:4))
14141414
@test typeof(x) == typeof(z)
14151415
@test z == x
14161416

@@ -1419,7 +1419,7 @@ end
14191419
z = convert(Vector, y)
14201420
@test typeof(x) == typeof(z)
14211421
@test z x
1422-
z = Vector(view(x, 1:4))
1422+
z = convert(Vector, view(y, 1:4))
14231423
@test typeof(x) == typeof(z)
14241424
@test z x
14251425

@@ -1428,7 +1428,7 @@ end
14281428
z = convert(Matrix, y)
14291429
@test typeof(x) == typeof(z)
14301430
@test z == x
1431-
z = Matrix(view(x, :, 1:4))
1431+
z = convert(Matrix, view(y, :, 1:4))
14321432
@test typeof(x) == typeof(z)
14331433
@test z == x
14341434

@@ -1437,7 +1437,7 @@ end
14371437
z = convert(Matrix, y)
14381438
@test typeof(x) == typeof(z)
14391439
@test z x
1440-
z = Matrix(view(x, :, 1:4))
1440+
z = convert(Matrix, view(y, :, 1:4))
14411441
@test typeof(x) == typeof(z)
14421442
@test z x
14431443
end
@@ -1462,6 +1462,119 @@ end
14621462
@test z x
14631463
end
14641464

1465+
@testset "Array(::CatArrOrSub{T<:CategoricalValue}) produces Array{T}" begin
1466+
x = [1,1,2,2]
1467+
y = categorical(x)
1468+
z = Array{eltype(y)}(y)
1469+
@test z isa Array{eltype(y)}
1470+
@test z == x
1471+
z = Array{eltype(y)}(view(y, 1:4))
1472+
@test z isa Array{eltype(y)}
1473+
@test z == x
1474+
1475+
x = [1,1,2,missing]
1476+
y = categorical(x)
1477+
z = Array{eltype(y)}(y)
1478+
@test z isa Array{eltype(y)}
1479+
@test z x
1480+
z = Array{eltype(y)}(view(y, 1:4))
1481+
@test z isa Array{eltype(y)}
1482+
@test z x
1483+
1484+
x = [1,1,2,2]
1485+
y = categorical(x)
1486+
z = Vector{eltype(y)}(y)
1487+
@test z isa Vector{eltype(y)}
1488+
@test z == x
1489+
z = Vector{eltype(y)}(view(y, 1:4))
1490+
@test z isa Vector{eltype(y)}
1491+
@test z == x
1492+
1493+
x = [1,1,2,missing]
1494+
y = categorical(x)
1495+
z = Vector{eltype(y)}(y)
1496+
@test z isa Vector{eltype(y)}
1497+
@test z x
1498+
z = Vector{eltype(y)}(view(y, 1:4))
1499+
@test z isa Vector{eltype(y)}
1500+
@test z x
1501+
1502+
x = [1 1 2 2]
1503+
y = categorical(x)
1504+
z = Matrix{eltype(y)}(y)
1505+
@test z isa Matrix{eltype(y)}
1506+
@test z == x
1507+
z = Matrix{eltype(y)}(view(y, :, 1:4))
1508+
@test z isa Matrix{eltype(y)}
1509+
@test z == x
1510+
1511+
x = [1 1 2 missing]
1512+
y = categorical(x)
1513+
z = Matrix{eltype(y)}(y)
1514+
@test z isa Matrix{eltype(y)}
1515+
@test z x
1516+
z = Matrix{eltype(y)}(view(y, :, 1:4))
1517+
@test z isa Matrix{eltype(y)}
1518+
@test z x
1519+
end
1520+
1521+
1522+
@testset "convert(Array, ::CatArrOrSub{T<:CategoricalValue}) produces Array{T}" begin
1523+
x = [1,1,2,2]
1524+
y = categorical(x)
1525+
z = convert(Array{eltype(y)}, y)
1526+
@test z isa Array{eltype(y)}
1527+
@test z == x
1528+
z = convert(Array{eltype(y)}, view(y, 1:4))
1529+
@test z isa Array{eltype(y)}
1530+
@test z == x
1531+
1532+
x = [1,1,2,missing]
1533+
y = categorical(x)
1534+
z = convert(Array{eltype(y)}, y)
1535+
@test z isa Array{eltype(y)}
1536+
@test z x
1537+
z = convert(Array{eltype(y)}, view(y, 1:4))
1538+
@test z isa Array{eltype(y)}
1539+
@test z x
1540+
1541+
x = [1,1,2,2]
1542+
y = categorical(x)
1543+
z = convert(Vector{eltype(y)}, y)
1544+
@test z isa Vector{eltype(y)}
1545+
@test z == x
1546+
z = convert(Vector{eltype(y)}, view(y, 1:4))
1547+
@test z isa Vector{eltype(y)}
1548+
@test z == x
1549+
1550+
x = [1,1,2,missing]
1551+
y = categorical(x)
1552+
z = convert(Vector{eltype(y)}, y)
1553+
@test z isa Vector{eltype(y)}
1554+
@test z x
1555+
z = convert(Vector{eltype(y)}, view(y, 1:4))
1556+
@test z isa Vector{eltype(y)}
1557+
@test z x
1558+
1559+
x = [1 1 2 2]
1560+
y = categorical(x)
1561+
z = convert(Matrix{eltype(y)}, y)
1562+
@test z isa Matrix{eltype(y)}
1563+
@test z == x
1564+
z = convert(Matrix{eltype(y)}, view(y, :, 1:4))
1565+
@test z isa Matrix{eltype(y)}
1566+
@test z == x
1567+
1568+
x = [1 1 2 missing]
1569+
y = categorical(x)
1570+
z = convert(Matrix{eltype(y)}, y)
1571+
@test z isa Matrix{eltype(y)}
1572+
@test z x
1573+
z = convert(Matrix{eltype(y)}, view(y, :, 1:4))
1574+
@test z isa Matrix{eltype(y)}
1575+
@test z x
1576+
end
1577+
14651578
@testset "convert(AbstractArray{T}, x)" begin
14661579
x = [1,1,2,2]
14671580
y = categorical(x)

0 commit comments

Comments
 (0)