25

Is there a function in Julia that returns a copy of an array in a desired type, i.e., an equivalent of numpys astype function? I have an "Any" type array, and want to convert it to a Float array. I tried:

new_array = Float64(array)

but I get the following error

LoadError: MethodError: `convert` has no method matching 
convert(::Type{Float64}, ::Array{Any,2})
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert(::Type{Float64}, !Matched::Int8)
  convert(::Type{Float64}, !Matched::Int16)
  ...
  while loading In[140], in expression starting on line 1

  in call at essentials.jl:56

I can just write a function that goes through the array and returns a float value of each element, but I find it a little odd if there's no built-in method to do this.

1
  • There are several answers below that seem to be correct. You should select whichever you think is best as the answer. Commented Oct 8, 2018 at 17:25

4 Answers 4

25

Use convert. Note the syntax I used for the first array; if you know what you want before the array is created, you can declare the type in front of the square brackets. Any just as easily could've been replaced with Float64 and eliminated the need for the convert function.

julia> a = Any[1.2, 3, 7]
3-element Array{Any,1}:
 1.2
 3  
 7  

julia> convert(Array{Float64,1}, a)
3-element Array{Float64,1}:
 1.2
 3.0
 7.0
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, what does ,1 do in {Float64,1}?
It specifies the dimension of the array, in this case, 1-dimensional (i.e. Vector)
Thanks, it also seems fine to omit it when it is 1.
19

You can also use the broadcast operator .:

a = Any[1.2, 3, 7]
Float64.(a)

1 Comment

Thank you sir, I almost lost a hope to convert dataframe obtained via CSV.read to Int16 from default Int64. For some reason, "types" and "type" keyword arguments just ignored and you always get Int64 This worked for me df = Int16.(CSV.read("file.csv"))
13

You can use:

new_array = Array{Float64}(array)

Comments

2

Daniel and Randy's answers are solid, I'll just add another way here I like because it can make more complicated iterative cases relatively succinct. That being said, it's not as efficient as the other answers, which are more specifically related to conversion / type declaration. But since the syntax can be pretty easily extended to other use case it's worth adding:

a = Array{Any,1}(rand(1000))
f = [float(a[i]) for i = 1:size(a,1)]

1 Comment

I used this approach when converting string entries read in from CSV directly to get the values 't4new = [parse(t4[i]) for i=1:length(t4)]`. this gives me the array of int values I needed as you expected your use is extensible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.