Julia: Convert Column Type from Integer to Float64 to DataFrame

I am trying to change the type of numbers in a DataFrame column from an integer to a floating point. It should be simple, but it does not work. The data type remains integer. What am I missing?

In  [2]: using DataFrames
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])

Out [2]: 4x2 DataFrame
| Row | A | B   |
|-----|---|-----|
| 1   | 1 | "M" |
| 2   | 2 | "F" |
| 3   | 3 | "F" |
| 4   | 4 | "M" |

In  [3]: df[:,:A] = float64(df[:,:A])

Out [3]: 4-element DataArray{Float64,1}:
 1.0
 2.0
 3.0
 4.0

In  [4]: df

Out [4]: 4x2 DataFrame
| Row | A | B   |
|-----|---|-----|
| 1   | 1 | "M" |
| 2   | 2 | "F" |
| 3   | 3 | "F" |
| 4   | 4 | "M" |

In  [5]: typeof(df[:,:A])

Out [5]: DataArray{Int64,1} (constructor with 1 method)
+4
source share
2 answers

The reason for this is mutation and transformation. If you have two vectors

a = [1:3]
b = [4:6]

You can make a xlink to one of them with an appointment.

x = a

Now xthey abelong to the same vector [1, 2, 3]. If then assigned btox

x = b

Now you have changed xto a link to the same vector as b.

, .

x[:] = a

a b, [1, 2, 3].

. , Julia .

x[1] = 5.0

[5, 2, 3], Julia Float64 5.0 Int 5.

x[1] = 5.5

InexactError(), 5.5 .

DataFrames, , , DataFrame . , DataFrame

df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])

, [1, 2, 3, 4] ["M", "F", "F", "M"]. DataFrame .

,

df[:,:A] = float64(df[:,:A])

, [1, 2, 3, 4] Float64. , df[:A], Float64 Int, Int.

df[:A] = float64(df[:A])

, , , DataFrame, Flaot64.

, .

+4

:

df[:A] = float64(df[:A])

Julia v0.3.5 DataFrames v0.6.1.

. :

df[:, :A] = [2.0, 2.0, 3.0, 4.0]

[2,2,3,4], Int64,

df[:A] = [2.0, 2.0, 3.0, 4.0]

.

( , ). , , , .

+1

All Articles