[英]Convert Int64 array to Int8 in Julia gives InexactError: trunc(Int8, -9223372036854775808)
我尝试将 julia df 的一部分从 Int64 转换为 Int8。
输入:
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
另外: 输入:
maximum(array)
output:
11
所以这个的 bigges 值小于 255,这是 Int8 的最大大小。
谢谢,
贝内
虽然数据中的最大值为 11,但数据中的最小值为 -9223372036854775808,也称为typemin(Int64)
,无法转换为Int8
,因为该类型只能表示从 -128 到 127 的值。
有时数组中的typemin
值用于表示 null 值或缺失值。 这不是 Julia 中的最佳实践,但您的数据源可能正在以这种方式使用该值。
假设你有一个数组arr
:
julia> arr = [1, 2, typemin(Int), 4]
4-element Vector{Int64}:
1
2
-9223372036854775808
4
如果typemin
值应该表示缺失数据,您可以执行以下操作:
julia> [n == typemin(Int) ? missing : Int8(n) for n in arr]
4-element Vector{Union{Missing, Int8}}:
1
2
missing
4
或者,如果它应该指示空/无效数据,则可以不使用nothing
而不是上面missing
的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.