Remove leading zeros from string to int

So my data looks like:

Stay Duration
string3?
-------
001
012
missing
002

What I would like is someway to convert these to Int:

Stay Duration
Int64
-------
1
12
missing
2

My normal method of converting a string to int with missing values doesn’t work:

IntConverter(x) = Int64(x)
Column_Accepts_Missing = passmissing(IntConverter)
df[!, :stay_dur] = Column_Accepts_Missing.(df[!, :stay_dur])

LoadError: MethodError: no method matching Int64(::String3)

Is there a way to reverse lpad on a string?

Getting an integer from a string is parsing, so you should use parse:

julia> parse(Int64, "001")
1

In your case you’d have to combine it with passmissing as shown, so:

julia> passmissing(parse).(Int64, ["001", "012", missing, "002"])
4-element Vector{Union{Missing, Int64}}:
  1
 12
   missing
  2
3 Likes