Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -33,15 +33,15 @@ 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 }}
docs:
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:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ScientificTypes"
uuid = "321657f4-b219-11e9-178b-2701a2544e81"
authors = ["Anthony D. Blaom <anthony.blaom@gmail.com>"]
version = "3.3.0"
version = "3.4.0"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
4 changes: 4 additions & 0 deletions src/coerce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/convention/coerce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
#
Expand Down
1 change: 0 additions & 1 deletion test/coerce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 16 additions & 13 deletions test/convention/coerce.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# -----------------------------------------------------------------------------------------
##############
## 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]
@test scitype_union(coerce(A, Continuous)) == Continuous
@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]
Expand Down Expand Up @@ -155,4 +158,4 @@ end
c = coerce(x, OrderedFactor, tight=true)
@test c == categorical([1,2])
@test !(eltype(c) >: Missing)
end
end
Loading