Julia: mapslices errors with type unstable function (problematic when working with Missings)

Created on 27 Feb 2018  路  2Comments  路  Source: JuliaLang/julia

On Julia master (0.6 seems to have the same issue) mapslices expects a type stable function, which makes it hard to use it in combination with Missings. For example:

julia> M = [1 2; 3 4]
2脳2 Array{Int64,2}:
 1  2
 3  4

julia> mapslices(sum, M, 2)
2脳1 Array{Int64,2}:
 3
 7

julia> mapslices(v -> sum(v) == 3 ? missing : 1, M, 2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Missing
arrays bug missing data

Most helpful comment

Another situation where it's annoying is when the input contains missing values:

julia> M = [missing 2; 3 4]
2脳2 Array{Union{Missing, Int64},2}:
  missing  2
 3         4

julia> mapslices(sum, M, 2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Missing

This goes against the general convention that we don't throw errors in the presence of missing values in: we just propagate them when possible. The best solution to fix that would probably be to use the same approach as in map, i.e. widen the element type if needed. That would be technically (slightly) breaking, since a type-unstable function can work currently if conversion to the type of the first slice is possible (e.g. Int and Float64), and widening the type would give an abstract type (e.g. Real).

All 2 comments

Another situation where it's annoying is when the input contains missing values:

julia> M = [missing 2; 3 4]
2脳2 Array{Union{Missing, Int64},2}:
  missing  2
 3         4

julia> mapslices(sum, M, 2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Missing

This goes against the general convention that we don't throw errors in the presence of missing values in: we just propagate them when possible. The best solution to fix that would probably be to use the same approach as in map, i.e. widen the element type if needed. That would be technically (slightly) breaking, since a type-unstable function can work currently if conversion to the type of the first slice is possible (e.g. Int and Float64), and widening the type would give an abstract type (e.g. Real).

Triage feels that this is a bug fix rather than a genuine breaking change.

Was this page helpful?
0 / 5 - 0 ratings