Skip to content

Commit 2906394

Browse files
authored
Merge pull request #162 from juliohm/fix-161
Fix #161: Allow coercion with string names
2 parents 39e7aab + 60a0ec6 commit 2906394

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/coerce.jl

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const ColKey = Union{Symbol,AbstractString}
2+
13
"""
24
coerce(A, specs...; tight=false, verbosity=1)
35
@@ -16,7 +18,9 @@ both the `OldScitype` and `Union{Missing,OldScitype}` cases):
1618
1719
(iii) a dictionary of scientific types keyed on column names:
1820
19-
coerce(X, d::AbstractDict{Symbol, <:Type}; verbosity=1)
21+
coerce(X, d::AbstractDict{<:ColKey, <:Type}; verbosity=1)
22+
23+
where `ColKey = Union{Symbol,AbstractString}`.
2024
2125
### Examples
2226
@@ -50,7 +54,7 @@ coerce(::Val{:other}, X, a...; kw...) =
5054

5155
_bad_dictionary() = throw(ArgumentError(
5256
"A dictionary specifying a scitype conversion "*
53-
"must have type `AbstractDict{Symbol, <:Type}`. It's keys must "*
57+
"must have type `AbstractDict{<:ColKey, <:Type}`. It's keys must "*
5458
"be column names and its values be scientific types. "*
5559
"E.g., `Dict(:cats=>Continuous, :dogs=>Textual`. "))
5660
coerce(::Val{:table}, X, types_dict::AbstractDict; kw...) =
@@ -68,7 +72,7 @@ coerce(::Val{:table}, X, specs...; kw...) = _bad_specs()
6872

6973
function coerce(::Val{:table},
7074
X,
71-
types_dict::AbstractDict{Symbol, <:Type};
75+
types_dict::AbstractDict{<:ColKey, <:Type};
7276
kw...)
7377
isempty(types_dict) && return X
7478
names = schema(X).names
@@ -103,7 +107,7 @@ end
103107
# symbol=>type and type=>type pairs can be specified in place of a
104108
# dictionary:
105109

106-
feature_scitype_pairs(p::Pair{Symbol,<:Type}, X) = [p, ]
110+
feature_scitype_pairs(p::Pair{<:ColKey,<:Type}, X) = [Symbol(first(p)) => last(p), ]
107111
function feature_scitype_pairs(p::Pair{<:Type,<:Type}, X)
108112
from_scitype = first(p)
109113
to_scitype = last(p)
@@ -121,7 +125,7 @@ for c in (:coerce, :coerce!)
121125
ex = quote
122126
function $c(::Val{:table},
123127
X,
124-
mixed_pairs::Pair{<:Union{Symbol,<:Type},<:Type}...;
128+
mixed_pairs::Pair{<:Union{<:ColKey,<:Type},<:Type}...;
125129
kw...)
126130
components = map(p -> feature_scitype_pairs(p, X), mixed_pairs)
127131
pairs = vcat(components...)
@@ -170,7 +174,7 @@ coerce!(::Val{:table}, X, specs...; kw...) = _bad_specs()
170174

171175
function coerce!(::Val{:table},
172176
X,
173-
types_dict::AbstractDict{Symbol, <:Type};
177+
types_dict::AbstractDict{<:ColKey, <:Type};
174178
kw...)
175179
# DataFrame --> coerce_df!
176180
if is_type(X, :DataFrames, :DataFrame)
@@ -189,7 +193,7 @@ end
189193
190194
In place coercion for a dataframe.(Unexported method)
191195
"""
192-
function coerce_df!(df, tdict::AbstractDict{Symbol, <:Type}; kw...)
196+
function coerce_df!(df, tdict::AbstractDict{<:ColKey, <:Type}; kw...)
193197
names = schema(df).names
194198
for name in names
195199
name in keys(tdict) || continue

test/basic_tests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ end
214214
@test scitype_union(yc) == Union{Missing,OrderedFactor{3}}
215215
@test scitype_union(y) == Union{Missing,Multiclass{3}}
216216

217+
# tests fix for issue https://github.com/JuliaAI/ScientificTypes.jl/issues/161
218+
X = (x=10:10:44, y=1:4, z=collect("abcd"))
219+
Xc = coerce(X, :x => Continuous, "y" => Continuous)
220+
@test scitype_union(Xc.x) === Continuous
221+
@test scitype_union(Xc.y) === Continuous
217222
end
218223

219224
@testset "coerce arrays" begin

0 commit comments

Comments
 (0)