From 60a0ec68241bc6ff2829b96f57f710efaed6b252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Wed, 29 Sep 2021 10:13:34 -0300 Subject: [PATCH 1/3] Fix #161: Allow coercion with string names --- src/coerce.jl | 18 +++++++++++------- test/basic_tests.jl | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/coerce.jl b/src/coerce.jl index 8ea54ab..f5b3224 100644 --- a/src/coerce.jl +++ b/src/coerce.jl @@ -1,3 +1,5 @@ +const ColKey = Union{Symbol,AbstractString} + """ coerce(A, specs...; tight=false, verbosity=1) @@ -16,7 +18,9 @@ both the `OldScitype` and `Union{Missing,OldScitype}` cases): (iii) a dictionary of scientific types keyed on column names: - coerce(X, d::AbstractDict{Symbol, <:Type}; verbosity=1) + coerce(X, d::AbstractDict{<:ColKey, <:Type}; verbosity=1) + +where `ColKey = Union{Symbol,AbstractString}`. ### Examples @@ -50,7 +54,7 @@ coerce(::Val{:other}, X, a...; kw...) = _bad_dictionary() = throw(ArgumentError( "A dictionary specifying a scitype conversion "* - "must have type `AbstractDict{Symbol, <:Type}`. It's keys must "* + "must have type `AbstractDict{<:ColKey, <:Type}`. It's keys must "* "be column names and its values be scientific types. "* "E.g., `Dict(:cats=>Continuous, :dogs=>Textual`. ")) coerce(::Val{:table}, X, types_dict::AbstractDict; kw...) = @@ -68,7 +72,7 @@ coerce(::Val{:table}, X, specs...; kw...) = _bad_specs() function coerce(::Val{:table}, X, - types_dict::AbstractDict{Symbol, <:Type}; + types_dict::AbstractDict{<:ColKey, <:Type}; kw...) isempty(types_dict) && return X names = schema(X).names @@ -103,7 +107,7 @@ end # symbol=>type and type=>type pairs can be specified in place of a # dictionary: -feature_scitype_pairs(p::Pair{Symbol,<:Type}, X) = [p, ] +feature_scitype_pairs(p::Pair{<:ColKey,<:Type}, X) = [Symbol(first(p)) => last(p), ] function feature_scitype_pairs(p::Pair{<:Type,<:Type}, X) from_scitype = first(p) to_scitype = last(p) @@ -121,7 +125,7 @@ for c in (:coerce, :coerce!) ex = quote function $c(::Val{:table}, X, - mixed_pairs::Pair{<:Union{Symbol,<:Type},<:Type}...; + mixed_pairs::Pair{<:Union{<:ColKey,<:Type},<:Type}...; kw...) components = map(p -> feature_scitype_pairs(p, X), mixed_pairs) pairs = vcat(components...) @@ -170,7 +174,7 @@ coerce!(::Val{:table}, X, specs...; kw...) = _bad_specs() function coerce!(::Val{:table}, X, - types_dict::AbstractDict{Symbol, <:Type}; + types_dict::AbstractDict{<:ColKey, <:Type}; kw...) # DataFrame --> coerce_df! if is_type(X, :DataFrames, :DataFrame) @@ -189,7 +193,7 @@ end In place coercion for a dataframe.(Unexported method) """ -function coerce_df!(df, tdict::AbstractDict{Symbol, <:Type}; kw...) +function coerce_df!(df, tdict::AbstractDict{<:ColKey, <:Type}; kw...) names = schema(df).names for name in names name in keys(tdict) || continue diff --git a/test/basic_tests.jl b/test/basic_tests.jl index 4c8025a..539b6c0 100644 --- a/test/basic_tests.jl +++ b/test/basic_tests.jl @@ -214,6 +214,11 @@ end @test scitype_union(yc) == Union{Missing,OrderedFactor{3}} @test scitype_union(y) == Union{Missing,Multiclass{3}} + # tests fix for issue https://github.com/JuliaAI/ScientificTypes.jl/issues/161 + X = (x=10:10:44, y=1:4, z=collect("abcd")) + Xc = coerce(X, :x => Continuous, "y" => Continuous) + @test scitype_union(Xc.x) === Continuous + @test scitype_union(Xc.y) === Continuous end @testset "coerce arrays" begin From 60aa079b9632ee3e71093bf5935302e2bd56dd6e Mon Sep 17 00:00:00 2001 From: "Anthony Blaom, PhD" Date: Thu, 30 Sep 2021 08:52:33 +1300 Subject: [PATCH 2/3] bump 2.2.3 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a039c19..673eab0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ScientificTypes" uuid = "321657f4-b219-11e9-178b-2701a2544e81" authors = ["Anthony D. Blaom "] -version = "2.2.2" +version = "2.2.3" [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" From 2e864cb770c5e34cccdadf702283feab8d201952 Mon Sep 17 00:00:00 2001 From: "Anthony Blaom, PhD" Date: Thu, 30 Sep 2021 08:53:36 +1300 Subject: [PATCH 3/3] bump 2.3 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 673eab0..eda7c1e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ScientificTypes" uuid = "321657f4-b219-11e9-178b-2701a2544e81" authors = ["Anthony D. Blaom "] -version = "2.2.3" +version = "2.3" [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"