-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
An isconvertible check #3631
Comments
This would be great. I feel like this even came when I last wrote about the type inference code for DataFrames IO. Jeff's response, which was quite correct, was that it's generally easier to try the conversion and only report if it failed. In DataFrames IO, you'll see that we now do exactly that by always returning a triple: (1) converted value, (2) did conversion succeed, (3) was value missing. |
Trying the conversion and reporting if it failed is also ok - and may end up being higher performance too. I am open to other ideas too, but I feel like this is essential to have for most of the convert functions in Base. |
I totally agree. As I've said before, I think we need a lot more tools for type conversion in Base. |
This would be nice. It is a tricky design problem though, since having separate |
with eventual debugging support, we should be able to read the frame info and pop the arguments out of their stack locations. although that would require being able to do that rather expensive operation in before unwinding the stack for the exception handler perhaps this can be closed by #9316 (comment) and Nullable{Float64}, etc? |
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces following methods that parse a string as the indicated type and return a `Nullable` with the result instead of throwing exception: - `maybeint{T<:Integer}(::Type{T<:Integer},s::AbstractString)` - `maybefloat32(s::AbstractString)` and `maybefloat64(s::AbstractString)` Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces the tryparse method: - tryparse{T<:Integer}(::Type{T<:Integer},s::AbstractString) - tryparse(::Type{Float..},s::AbstractString) - a few variants of the above And: - tryparse(Float.., ...) call the corresponding C functions jl_try_strtof, jl_try_substrtof, jl_try_strtod and jl_try_substrtod. - The parseint, parsefloat, float64_isvalid and float32_isvalid methods wrap the corresponding tryparse methods. - The jl_strtod, jl_strtof, ... functions are wrappers over the jl_try_str... functions. This should fix JuliaLang#10498 as well. Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces the tryparse method: - tryparse{T<:Integer}(::Type{T<:Integer},s::AbstractString) - tryparse(::Type{Float..},s::AbstractString) - a few variants of the above And: - tryparse(Float.., ...) call the corresponding C functions jl_try_strtof, jl_try_substrtof, jl_try_strtod and jl_try_substrtod. - The parseint, parsefloat, float64_isvalid and float32_isvalid methods wrap the corresponding tryparse methods. - The jl_strtod, jl_strtof, ... functions are wrappers over the jl_try_str... functions. This should fix JuliaLang#10498 as well. Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Introduces the tryparse method: - tryparse{T<:Integer}(::Type{T<:Integer},s::AbstractString) - tryparse(::Type{Float..},s::AbstractString) - a few variants of the above And: - tryparse(Float.., ...) call the corresponding C functions jl_try_strtof, jl_try_substrtof, jl_try_strtod and jl_try_substrtod. - The parseint, parsefloat, float64_isvalid and float32_isvalid methods wrap the corresponding tryparse methods. - The jl_strtod, jl_strtof, ... functions are wrappers over the jl_try_str... functions. This should fix JuliaLang#10498 as well. Ref: discussions at JuliaLang#9316, JuliaLang#3631, JuliaLang#5704
Thoughts on whether we leave this open or close it? |
What was originally wanted seems to be addressed by |
Was gonna comment that |
Working with real world data, it is common to encounter poorly formatted files and such. One thing I find myself frequently doing is something like this in DataFrames:
The Age column should been integers, but there is some bad data in the column, as a result of which
readtable
left the column as a string. When I try to useint
, it is obvious that the data is corrupt, but I have no idea where it is corrupt.I often find myself doing something like:
[ try int(df["Age"][i]) catch end for i in 1:nrow(df) ]
I think that an
isconvertible
function would be generally useful, which takes the same arguments asconvert
, but returns a boolean based on whether the conversion is possible or not.The text was updated successfully, but these errors were encountered: