From 15509b22d7d569ff28dd2dba6eaa473df601d75d Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Fri, 1 Aug 2025 14:09:17 +0200 Subject: [PATCH 1/3] 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. --- src/array.jl | 2 - test/13_arraycommon.jl | 137 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 125 insertions(+), 14 deletions(-) diff --git a/src/array.jl b/src/array.jl index 4d47e82c..6752875b 100644 --- a/src/array.jl +++ b/src/array.jl @@ -411,8 +411,6 @@ convert(::Type{CategoricalArray{T, N}}, A::CategoricalArray{T, N}) where {T, N} convert(::Type{CategoricalArray{T}}, A::CategoricalArray{T}) where {T} = A convert(::Type{CategoricalArray}, A::CategoricalArray) = A -convert(::Type{Array{S, N}}, A::CatArrOrSub{T, N}) where {S, T, N} = - collect(S, A) convert(::Type{Array}, A::CatArrOrSub) = unwrap.(A) convert(::Type{Vector}, A::CatArrOrSub) = unwrap.(A) convert(::Type{Matrix}, A::CatArrOrSub) = unwrap.(A) diff --git a/test/13_arraycommon.jl b/test/13_arraycommon.jl index e95be673..7196a628 100644 --- a/test/13_arraycommon.jl +++ b/test/13_arraycommon.jl @@ -1336,7 +1336,7 @@ end z = Array(y) @test typeof(x) == typeof(z) @test z == x - z = Array(view(x, 1:4)) + z = Array(view(y, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1345,7 +1345,7 @@ end z = Array(y) @test typeof(x) == typeof(z) @test z ≅ x - z = Array(view(x, 1:4)) + z = Array(view(y, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x @@ -1354,7 +1354,7 @@ end z = Vector(y) @test typeof(x) == typeof(z) @test z == x - z = Vector(view(x, 1:4)) + z = Vector(view(y, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1363,7 +1363,7 @@ end z = Vector(y) @test typeof(x) == typeof(z) @test z ≅ x - z = Vector(view(x, 1:4)) + z = Vector(view(y, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x @@ -1372,7 +1372,7 @@ end z = Matrix(y) @test typeof(x) == typeof(z) @test z == x - z = Matrix(view(x, :, 1:4)) + z = Matrix(view(y, :, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1381,7 +1381,7 @@ end z = Matrix(y) @test typeof(x) == typeof(z) @test z ≅ x - z = Matrix(view(x, :, 1:4)) + z = Matrix(view(y, :, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x end @@ -1392,7 +1392,7 @@ end z = convert(Array, y) @test typeof(x) == typeof(z) @test z == x - z = Array(view(x, 1:4)) + z = convert(Array, view(y, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1401,7 +1401,7 @@ end z = convert(Array, y) @test typeof(x) == typeof(z) @test z ≅ x - z = Array(view(x, 1:4)) + z = convert(Array, view(y, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x @@ -1410,7 +1410,7 @@ end z = convert(Vector, y) @test typeof(x) == typeof(z) @test z == x - z = Vector(view(x, 1:4)) + z = convert(Vector, view(y, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1419,7 +1419,7 @@ end z = convert(Vector, y) @test typeof(x) == typeof(z) @test z ≅ x - z = Vector(view(x, 1:4)) + z = convert(Vector, view(y, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x @@ -1428,7 +1428,7 @@ end z = convert(Matrix, y) @test typeof(x) == typeof(z) @test z == x - z = Matrix(view(x, :, 1:4)) + z = convert(Matrix, view(y, :, 1:4)) @test typeof(x) == typeof(z) @test z == x @@ -1437,7 +1437,7 @@ end z = convert(Matrix, y) @test typeof(x) == typeof(z) @test z ≅ x - z = Matrix(view(x, :, 1:4)) + z = convert(Matrix, view(y, :, 1:4)) @test typeof(x) == typeof(z) @test z ≅ x end @@ -1462,6 +1462,119 @@ end @test z ≅ x end +@testset "Array(::CatArrOrSub{T<:CategoricalValue}) produces Array{T}" begin + x = [1,1,2,2] + y = categorical(x) + z = Array{eltype(y)}(y) + @test z isa Array{eltype(y)} + @test z == x + z = Array{eltype(y)}(view(y, 1:4)) + @test z isa Array{eltype(y)} + @test z == x + + x = [1,1,2,missing] + y = categorical(x) + z = Array{eltype(y)}(y) + @test z isa Array{eltype(y)} + @test z ≅ x + z = Array{eltype(y)}(view(y, 1:4)) + @test z isa Array{eltype(y)} + @test z ≅ x + + x = [1,1,2,2] + y = categorical(x) + z = Vector{eltype(y)}(y) + @test z isa Vector{eltype(y)} + @test z == x + z = Vector{eltype(y)}(view(y, 1:4)) + @test z isa Vector{eltype(y)} + @test z == x + + x = [1,1,2,missing] + y = categorical(x) + z = Vector{eltype(y)}(y) + @test z isa Vector{eltype(y)} + @test z ≅ x + z = Vector{eltype(y)}(view(y, 1:4)) + @test z isa Vector{eltype(y)} + @test z ≅ x + + x = [1 1 2 2] + y = categorical(x) + z = Matrix{eltype(y)}(y) + @test z isa Matrix{eltype(y)} + @test z == x + z = Matrix{eltype(y)}(view(y, :, 1:4)) + @test z isa Matrix{eltype(y)} + @test z == x + + x = [1 1 2 missing] + y = categorical(x) + z = Matrix{eltype(y)}(y) + @test z isa Matrix{eltype(y)} + @test z ≅ x + z = Matrix{eltype(y)}(view(y, :, 1:4)) + @test z isa Matrix{eltype(y)} + @test z ≅ x +end + + +@testset "convert(Array, ::CatArrOrSub{T<:CategoricalValue}) produces Array{T}" begin + x = [1,1,2,2] + y = categorical(x) + z = convert(Array{eltype(y)}, y) + @test z isa Array{eltype(y)} + @test z == x + z = convert(Array{eltype(y)}, view(y, 1:4)) + @test z isa Array{eltype(y)} + @test z == x + + x = [1,1,2,missing] + y = categorical(x) + z = convert(Array{eltype(y)}, y) + @test z isa Array{eltype(y)} + @test z ≅ x + z = convert(Array{eltype(y)}, view(y, 1:4)) + @test z isa Array{eltype(y)} + @test z ≅ x + + x = [1,1,2,2] + y = categorical(x) + z = convert(Vector{eltype(y)}, y) + @test z isa Vector{eltype(y)} + @test z == x + z = convert(Vector{eltype(y)}, view(y, 1:4)) + @test z isa Vector{eltype(y)} + @test z == x + + x = [1,1,2,missing] + y = categorical(x) + z = convert(Vector{eltype(y)}, y) + @test z isa Vector{eltype(y)} + @test z ≅ x + z = convert(Vector{eltype(y)}, view(y, 1:4)) + @test z isa Vector{eltype(y)} + @test z ≅ x + + x = [1 1 2 2] + y = categorical(x) + z = convert(Matrix{eltype(y)}, y) + @test z isa Matrix{eltype(y)} + @test z == x + z = convert(Matrix{eltype(y)}, view(y, :, 1:4)) + @test z isa Matrix{eltype(y)} + @test z == x + + x = [1 1 2 missing] + y = categorical(x) + z = convert(Matrix{eltype(y)}, y) + @test z isa Matrix{eltype(y)} + @test z ≅ x + z = convert(Matrix{eltype(y)}, view(y, :, 1:4)) + @test z isa Matrix{eltype(y)} + @test z ≅ x +end + @testset "convert(AbstractArray{T}, x)" begin x = [1,1,2,2] y = categorical(x) From 337faebe3d48b61be0c56e821cffd3a8ab5fd368 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Fri, 1 Aug 2025 14:10:04 +0200 Subject: [PATCH 2/3] Bump version to 1.0.1 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 83d5ba30..9c0bd917 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "CategoricalArrays" uuid = "324d7699-5711-5eae-9e2f-1d82baa6b597" -version = "1.0.0" +version = "1.0.1" [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" From 6ad3908db5920fc647ea441d6fb0514cb2a6587e Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Fri, 1 Aug 2025 14:12:30 +0200 Subject: [PATCH 3/3] Update NEWS.md for 1.0.1 --- NEWS.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/NEWS.md b/NEWS.md index 61842496..bf651a54 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# CategoricalArrays.jl v1.0.1 Release Notes + +## Bug fixes + +* Fix `Array{<:CategoricalValue}` constructors and `convert` to return an `Array` + rather than a `CategoricalArray` + ([#427](https://github.com/JuliaData/CategoricalArrays.jl/pull/427)). + + # CategoricalArrays.jl v1.0.0 Release Notes ## Breaking changes