JuliaData / TypedTables.jl

Simple, fast, column-based storage for data analysis in Julia
Other
145 stars 25 forks source link

append! NamedTuple to Table lead to strange behavior #116

Open femtotrader opened 2 months ago

femtotrader commented 2 months ago

Hello,

I was trying to append! a NamedTuple to Table... and noticed this odd behiavor


julia> t = Table(a = [1, 2, 3], b = [2.0, 4.0, 6.0])
Table with 2 columns and 3 rows:
     a  b
   ┌───────
 1 │ 1  2.0
 2 │ 2  4.0
 3 │ 3  6.0

julia> append!(t, (a=4, b=8.0))
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type @NamedTuple{a::Int64, b::Float64}

Closest candidates are:
  convert(::Type{NT}, ::NT) where {names, T<:Tuple, NT<:NamedTuple{names, T}}
   @ Base namedtuple.jl:184
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  convert(::Type{NT}, ::NamedTuple{names}) where {names, T<:Tuple, NT<:NamedTuple{names, T}}
   @ Base namedtuple.jl:186
  ...

Stacktrace:
 [1] setindex!
   @ C:\Users\femto\.julia\packages\TypedTables\ItVth\src\Table.jl:142 [inlined]
 [2] _append!
   @ .\array.jl:1201 [inlined]
 [3] append!(a::Table{@NamedTuple{…}, 1, @NamedTuple{…}}, iter::@NamedTuple{a::Int64, b::Float64})
   @ Base .\array.jl:1187
 [4] top-level scope
   @ REPL[31]:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> t
Table with 2 columns and 5 rows:
     a  b
   ┌────────────
 1 │ 1  2.0
 2 │ 2  4.0
 3 │ 3  6.0
 4 │ 3  1.0e-323
 5 │ 3  1.0e-323

These 2 rows are intriguing

 4 │ 3  1.0e-323
 5 │ 3  1.0e-323

Any idea what is causing that?

Would it be possible to append! a NamedTuple to a Table (without creating a Table from NamedTuple)

Kind regards

andyferris commented 2 months ago

Interesting.

The correct operation here is push! since you are attempting to add a single row to a collection of rows (and appending is for merging two tables).

I’m not sure what’s going on with the behavior above, I would need to take a look.

femtotrader commented 2 months ago

Thanks @andyferris

julia> push!(t, (a=4, b=8.0))
Table with 2 columns and 4 rows:
     a  b
   ┌───────
 1 │ 1  2.0
 2 │ 2  4.0
 3 │ 3  6.0
 4 │ 4  8.0

works fine.