1

I try to convert a part of an julia df from Int64 to Int8.

input:

array = convert(Array{Int8} , array );

output:

InexactError: trunc(Int8, -9223372036854775808)

Stacktrace:
  [1] throw_inexacterror(f::Symbol, #unused#::Type{Int8}, val::Int64)
    @ Core ./boot.jl:612
  [2] checked_trunc_sint
    @ ./boot.jl:634 [inlined]
  [3] toInt8
    @ ./boot.jl:649 [inlined]
  [4] Int8
    @ ./boot.jl:759 [inlined]
  [5] convert
    @ ./number.jl:7 [inlined]
  [6] setindex!
    @ ./array.jl:903 [inlined]
  [7] _unsafe_copyto!(dest::Vector{Int8}, doffs::Int64, src::Vector{Int64}, soffs::Int64, n::Int64)
    @ Base ./array.jl:253
  [8] unsafe_copyto!
    @ ./array.jl:307 [inlined]
  [9] _copyto_impl!
    @ ./array.jl:331 [inlined]
 [10] copyto!
    @ ./array.jl:317 [inlined]
 [11] copyto!
    @ ./array.jl:343 [inlined]
 [12] copyto_axcheck!
    @ ./abstractarray.jl:1104 [inlined]
 [13] Vector{Int8}(x::Vector{Int64})
    @ Base ./array.jl:563
 [14] Array
    @ ./boot.jl:482 [inlined]
 [15] convert(#unused#::Type{Array{Int8}}, a::Vector{Int64})
    @ Base ./array.jl:554
 [16] top-level scope
    @ In[62]:1
 [17] eval
    @ ./boot.jl:373 [inlined]
 [18] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

Also: Input:

maximum(array)

output:

11

So the bigges value of this is under 255 which is the max size of Int8.

thanks,

Bene

Benedikt Schwab
  • 49
  • 1
  • 1
  • 3

2 Answers2

1

While the maximum value in your data is 11, the minimum value in your data is -9223372036854775808, also known as typemin(Int64), which cannot be converted to Int8 since that type can only represent values from -128 to 127.

StefanKarpinski
  • 32,404
  • 10
  • 86
  • 111
0

Sometimes the typemin value in the array is used to indicate a null value or a missing value. This isn't the best practice in Julia, but it's possible your data source is using the value this way.

Say you have an array arr:

julia> arr = [1, 2, typemin(Int), 4]
4-element Vector{Int64}:
                    1
                    2
 -9223372036854775808
                    4

If the typemin value is supposed to indicate missing data, you can do:

julia> [n == typemin(Int) ? missing : Int8(n) for n in arr]
4-element Vector{Union{Missing, Int8}}:
 1
 2
  missing
 4

Or if it's supposed to indicate null/invalid data, you can use nothing instead of missing above.

Sundar R
  • 13,776
  • 6
  • 49
  • 76