JuliaAI / ScientificTypes.jl

An API for dispatching on the "scientific" type of data instead of the machine type
MIT License
96 stars 8 forks source link

`tight` keyword not working for in-place coercion #166

Closed ablaom closed 2 years ago

ablaom commented 2 years ago
julia> using DataFrames, ScientificTypes

julia> v = [1, 2, 3, 4, missing][1:4]
4-element Vector{Union{Missing, Int64}}:
 1
 2
 3
 4

julia> df = DataFrame(v=v);

# have `Missing` in eltypes:
julia> schema(df)
┌─────────┬───────────────────────┬───────────────────────┐
│ _.names │ _.types               │ _.scitypes            │
├─────────┼───────────────────────┼───────────────────────┤
│ v       │ Union{Missing, Int64} │ Union{Missing, Count} │
└─────────┴───────────────────────┴───────────────────────┘
_.nrows = 4

 # this works:
julia> coerce(df, Union{Missing,Count} => Count, tight=true) |> schema
┌─────────┬─────────┬────────────┐
│ _.names │ _.types │ _.scitypes │
├─────────┼─────────┼────────────┤
│ v       │ Int64   │ Count      │
└─────────┴─────────┴────────────┘
_.nrows = 4

# this does not:
julia> coerce!(df, Union{Missing,Count} => Count, tight=true) |> schema
ERROR: ScientificTypes.CoercionError("`coerce` is undefined for non-tabular data.")
Stacktrace:
  [1] coerce(::Val{:other}, ::Vector{Union{Missing, Int64}}, ::Type, ::Vararg{Any, N} where N; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:51
  [2] coerce(::Val{:other}, ::Vector{Union{Missing, Int64}}, ::Type, ::Pair{Symbol, Bool})
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:51
  [3] coerce(::Vector{Union{Missing, Int64}}, ::Type, ::Vararg{Any, N} where N; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})                   
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:48
  [4] coerce(::Vector{Union{Missing, Int64}}, ::Type, ::Pair{Symbol, Bool})
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:48
  [5] top-level scope
    @ ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:208
  [6] eval
    @ ./boot.jl:360 [inlined]
  [7] eval
    @ ~/.julia/packages/ScientificTypes/R7amQ/src/ScientificTypes.jl:1 [inlined]
  [8] coerce_df!(df::DataFrame, tdict::Dict{Symbol, Type}; kw::Base.Iterators.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tight,), Tuple{Bool}}})                          
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:210
  [9] coerce!(::Val{:table}, X::DataFrame, types_dict::Dict{Symbol, Type}; kw::Base.Iterators.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tight,), Tuple{Bool}}})      
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:181
 [10] coerce!(X::DataFrame, a::Dict{Symbol, Type}; kw::Base.Iterators.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tight,), Tuple{Bool}}})                          
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:165
 [11] coerce!(::Val{:table}, X::DataFrame, mixed_pairs::Pair{Union, DataType}; kw::Base.Iterators.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tight,), Tuple{Bool}}})      
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:147
 [12] coerce!(X::DataFrame, a::Pair{Union, DataType}; kw::Base.Iterators.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:tight,), Tuple{Bool}}})                          
    @ ScientificTypes ~/.julia/packages/ScientificTypes/R7amQ/src/coerce.jl:165
 [13] top-level scope
    @ REPL[33]:2