diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b48ba3d..b598daa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: arch: aarch64 version: 1 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - uses: julia-actions/setup-julia@v2 with: version: ${{ matrix.version }} @@ -33,7 +33,7 @@ jobs: - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v6 + - uses: codecov/codecov-action@v7 with: files: lcov.info token: ${{ secrets.CODECOV_TOKEN }} @@ -41,7 +41,7 @@ jobs: name: Documentation runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - uses: julia-actions/julia-buildpkg@latest - uses: julia-actions/julia-docdeploy@latest env: diff --git a/Project.toml b/Project.toml index 9d6ef17..0a44c48 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ScientificTypes" uuid = "321657f4-b219-11e9-178b-2701a2544e81" authors = ["Anthony D. Blaom "] -version = "3.3.0" +version = "3.4.0" [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" diff --git a/src/coerce.jl b/src/coerce.jl index 6ba40df..847c9bd 100644 --- a/src/coerce.jl +++ b/src/coerce.jl @@ -37,6 +37,10 @@ both the `OldScitype` and `Union{Missing,OldScitype}` cases): where `ColKey = Union{Symbol,AbstractString}`. +When the element type of `X` is `Union{Missing,T}` but no `missing` value is present, it +may be that `Missing` persists in the output element type. To force tightening of the +element type to exclude `Missing`, set `tight=true`. + ### Examples Specifying `column_name=>Scitype` pairs: diff --git a/src/convention/coerce.jl b/src/convention/coerce.jl index f2698f3..7876e45 100644 --- a/src/convention/coerce.jl +++ b/src/convention/coerce.jl @@ -17,6 +17,23 @@ coerce(X::AbstractArray, ::Type{T}; kw...) where {T} = "scientific type. ")) +# ------------------------------------------------------------------------ +# TEXTUAL + +# Arr{T} -> Textual +function coerce( + y::Arr{T}, + T2::Type{<:Union{Missing,Textual}}; + verbosity::Int=1, + tight::Bool=false, + ) where T + y = map(y) do η + ismissing(η) ? η : string(η) + end + _check_eltype(y, T2, verbosity) + return y +end + # ------------------------------------------------------------------------ # FINITE @@ -125,6 +142,7 @@ function coerce(y::Arr{T}, T2::Type{<:Union{Missing,C}}; end ## ARRAY OF ANY + # Note: in the categorical case, we don't care, because we broadcast anyway. # see CArr --> C above. # diff --git a/test/coerce.jl b/test/coerce.jl index 15e7ec4..55706d0 100644 --- a/test/coerce.jl +++ b/test/coerce.jl @@ -2,7 +2,6 @@ X = (x=10:10:44, y=1:4, z=collect("abcd")) @test_throws ScientificTypes.CoercionError coerce(X, :x=>Float64) - @test_throws ScientificTypes.CoercionError coerce(X, :x=>Textual) types = Dict(:x => Continuous, :z => Multiclass) X_coerced = coerce(X, types) diff --git a/test/convention/coerce.jl b/test/convention/coerce.jl index c8036f8..79a2f2b 100644 --- a/test/convention/coerce.jl +++ b/test/convention/coerce.jl @@ -1,14 +1,18 @@ -# ----------------------------------------------------------------------------------------- -############## -## Basic (Behavioural tests) -############## +@testset "coerce arrays" begin -# ---------------------------------------------------------------------------------------------------------------------- -############### -## Detailed Tests -############### + # coercing to Textual and resolution of #193: + y = [1, 2, 3, missing] + yout = @test_logs( + (:info, r"^Trying to"), + coerce(y, Textual) + ) + @test yout[1:3] == ["1", "2", "3"] + @test ismissing(yout[end]) + y = [1, 2, 3] + yout = coerce(y, Textual) + @test yout == ["1", "2", "3"] + @test coerce(yout, Textual) == yout -@testset "coerce arrays" begin A = rand(Int, 2, 3) z = rand(Char, 2, 3) y = Any[1.0 2; 3 4] @@ -16,11 +20,10 @@ @test scitype_union(coerce(A, OrderedFactor)) <: OrderedFactor @test scitype_union(coerce(z, Multiclass)) <: Multiclass @test scitype_union(coerce(y, Count)) === Count - - # test fix for issue 39 + + # test fix for https://github.com/JuliaAI/MLJScientificTypes.jl/issues/39 y = collect(Int64, 1:5) @test_throws ScientificTypes.CoercionError coerce(y, Float64) - @test_throws ScientificTypes.CoercionError coerce(y, Textual) @testset "coersion of Real->OrderedFactor" begin v = [0.1, 0.2, 0.2, 0.3, missing, 0.1] @@ -155,4 +158,4 @@ end c = coerce(x, OrderedFactor, tight=true) @test c == categorical([1,2]) @test !(eltype(c) >: Missing) -end \ No newline at end of file +end