Julia: tryparse should have argument to return missing

Created on 20 Jun 2019  ยท  3Comments  ยท  Source: JuliaLang/julia

Hello,

Following https://github.com/JuliaData/DataFrames.jl/issues/1854 I wonder if tryparse implementation shouldn't have an argument to output missing instead of nothing.

So it will be possible to simply do

julia> using DataFrames
julia> df = DataFrame(:A=>["", "2", "3"], :B=>[1.1, 2.2, 3.3])
3ร—2 DataFrame
โ”‚ Row โ”‚ A      โ”‚ B       โ”‚
โ”‚     โ”‚ String โ”‚ Float64 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1   โ”‚        โ”‚ 1.1     โ”‚
โ”‚ 2   โ”‚ 2      โ”‚ 2.2     โ”‚
โ”‚ 3   โ”‚ 3      โ”‚ 3.3     โ”‚
julia> df.A = tryparse.(Int, df.A, missing)
3-element Array{Union{Missing, Int64},1}:
  missing
 2
 3
julia> df
3ร—2 DataFrame
โ”‚ Row โ”‚ A       โ”‚ B       โ”‚
โ”‚     โ”‚ Int64โฐ  โ”‚ Float64 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1   โ”‚ missing โ”‚ 1.1     โ”‚
โ”‚ 2   โ”‚ 2       โ”‚ 2.2     โ”‚
โ”‚ 3   โ”‚ 3       โ”‚ 3.3     โ”‚

Your opinion?

Kind regards

Most helpful comment

Yeah, this should be as simple as doing mytryparse(T, str) = something(tryparse(T, str), missing)

All 3 comments

The value is not missing here. If you want missing just wrap tryparse in a function that transform nothing to missing.

Yeah, this should be as simple as doing mytryparse(T, str) = something(tryparse(T, str), missing)

Maybe the discussion should continue in https://github.com/JuliaData/Missings.jl/issues/61

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ararslan picture ararslan  ยท  3Comments

omus picture omus  ยท  3Comments

omus picture omus  ยท  3Comments

iamed2 picture iamed2  ยท  3Comments

TotalVerb picture TotalVerb  ยท  3Comments