I want to extract and convert to integer a column in a DataFrame



With Julia 0.5, I could do this:
Calories=int(Nutrition[2:11,2]), where Nutrition is a DataFrame.

With Julia 0.6, whatever I do of the kind spawns an ominous red box which floats over my program like a giant cloud in an otherwise sunny sky.


Use the broadcasted form of convert:

df = DataFrame(a = [1.0, 2.0, 3.0], b=[2.0, 3.0, 4.0])
convert.(Int64, df[2:3,:a])

results in

2-element DataArrays.DataArray{Int64,1}:


I am not sure I got your question, but the way you convert pieces of dataframes to arrays is by using:

A = convert(Array, df)


I tried this, but there must be something wrong with the file, since I got the following error message ->

LoadError: MethodError: Cannot convert an object of type String to an object of type Int64
This may have arisen from a call to the constructor Int64(…),
since type constructors fall back to convert methods.


Sounds like you need parse in place of convert.


@Jean-Victor, have you tried the simple convert(Array, df[1:2,:a]) I mentioned? It will convert the column preserving the eltype. In most cases, you don’t need to do this though, DataArrays returned by indexing DataFrames work seamlessly with many operations defined on AbstractArray


I tried this, but unfortunately it is interpreted as a string vector, yielding this error message when I use it as if it was an integer vector ->

LoadError: MethodError: no method matching *(::JuMP.Variable, ::String)


I think you need to tell us more about the structure of the Nutrition data frame on Julia 0.5 vs. 0.6, and how you create it.


There was something wrong with the file: the title had 2 lines. With Julia 0.5, using the same file, skipping the second line of the title was possible before conversion was attempted, whereas it doesn’t seem to be possible in Julia 0.6 because the whole set of lines of the dataframe seems to be checked for consistency before converting even part of it containing lines that can be converted. Removing one of the lines of the title and selecting lines 1 to 10 instead of lines 2 to 11 solves the problem.

Using Julia 0.6, there was also a problem with empty lines at the end of the file because the conversion did not handle NAs (by default, I suppose), which I also removed before attempting the conversion.

With the cleaned up file, both of your solutions worked flawlessly. Thank you both!