Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tryparse should have argument to return missing #32378

Closed
scls19fr opened this issue Jun 20, 2019 · 4 comments
Closed

tryparse should have argument to return missing #32378

scls19fr opened this issue Jun 20, 2019 · 4 comments

Comments

@scls19fr
Copy link
Contributor

scls19fr commented Jun 20, 2019

Hello,

Following JuliaData/DataFrames.jl#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     │
│ 222.2     │
│ 333.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 │
├─────┼─────────┼─────────┤
│ 1missing1.1     │
│ 222.2     │
│ 333.3

Your opinion?

Kind regards

@KristofferC
Copy link
Sponsor Member

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

@quinnj
Copy link
Member

quinnj commented Jun 20, 2019

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

@scls19fr
Copy link
Contributor Author

Maybe the discussion should continue in JuliaData/Missings.jl#61

@ghost
Copy link

ghost commented Mar 30, 2021

Julia Version 1.5.4
Commit 69fcb5745b (2021-03-11 19:13 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Xeon(R) W-3275M CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake-avx512)

using

mytryparse(T, str) = something(tryparse(T, str), missing)
mytryparse(Int, missing)

throws

MethodError: no method matching tryparse(::Type{Int64}, ::Missing)
Closest candidates are:
  tryparse(::Type{T}, ::AbstractString; base) where T<:Integer at parse.jl:232

Stacktrace:
 [1] mytryparse(::Type{T} where T, ::Missing) at ./In[138]:1
 [2] top-level scope at In[139]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

I referred to https://discourse.julialang.org/t/i-have-a-dataframe-with-multiple-columns-of-type-union-missing-string-what-is-the-most-concise-manner-of-converting-the-non-missing-values-in-float/54205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants