bensw
bensw

Reputation: 3028

How can I change column data type from float to string in Julia?

I am trying to get a column in a dataframe form float to string. I have tried

df = readtable("data.csv", coltypes = {String, String, String, String, String, Float64, Float64, String});

but I got complained

syntax: { } vector syntax is discontinued

I also have tried

dfB[:serial] = string(dfB[:serial])

but it didn't work either. So, I'd like to know what would be the proper approach to change column data type in Julia.

thx

Upvotes: 2

Views: 2267

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

On your first attempt, Julia tells you what the problem is - you can't make a vector with {}, you need to use []. Also, the name of the keyword argument should be eltypes rather than coltypes.

On the second try, you don't have a float, you have a Vector of floats. So to change the type you need to change the type of all elements. In Julia, elementwise operations on vectors are generalized by the 'dot' syntax, e.g. string.(collect(dfB[:serial])) . The collect is needed currently to cast the DataArray to a normal Array first – this will fail if the DataArray contains NAs. IMHO the DataFrames interface is still rather wonky, so expect a few headaches like this ATM.

Upvotes: 4

Related Questions