# DataFrames: convert column data type

**URL:** <https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522>\
**Category:** Data\
**Tags:** type, dataframes, convert\
**Created:** [March 4, 2020, 3:03pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522 "2020-03-04T15:03:29Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 24, 2021, 9:46pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/21 "2021-01-24T21:46:29Z")

</div>

The syntax is not deprecated but it is not allowed and it errors, so most likely @wakimchris is using old versions of CSV.jl and DataFrames.jl. That is why I have asked about it.

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [January 24, 2021, 10:18pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/22 "2021-01-24T22:18:28Z")

</div>

CSV v0.5.7  
DataFrames v0.18.4

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [January 24, 2021, 10:23pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/23 "2021-01-24T22:23:46Z")

</div>

> [@pdeffebach](#):
>
> `df[:, :Id_internal]` , _not_ `df[:2, :Id_internal]`

When I tried  
`df[:,:Id_internal] = parse.(Int64,df[:2,:Id_internal])`  
and  
`df[:,:Id_internal] = tryparse.(Int64,df[:2,:Id_internal])`

I got  
`ERROR: MethodError: Cannot 'convert' and object of type Int64 to an object of type String`  
This was a strange message because I want to do to other way around

> [@pdeffebach](#):
>
> Can you please post a link to the guide you are using to learn DataFrames?

I am not using a guide to learn DataFrames but rather trying to debug it, but can’t figure it out

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [January 24, 2021, 10:27pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/24 "2021-01-24T22:27:51Z")

</div>

Would my syntax work on a newer version?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 24, 2021, 11:29pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/25 "2021-01-24T23:29:12Z")

</div>

Sorry, you need one more change I should have mentioned earlier

```julia
df[!,:Id_internal] = tryparse.(Int64,df[:,:Id_internal])

```

The `!` means your replace the column entirely.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 24, 2021, 11:29pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/26 "2021-01-24T23:29:54Z")

</div>

the syntax for selecting columns using `df[[:col1, :col2]]` will _not_ work. But everything else you are doing will be the same.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 24, 2021, 11:33pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/27 "2021-01-24T23:33:15Z")

</div>

It is crucial to update DataFrames.jl and CSV.jl to the newest versions. You have very old versions of both that contained many bugs.

The reason why `df[[:col1, :col2]]` does not work any more is that data frame is a 2-dimensional object, so we strictly require to pass both row and column index when subsetting a data frame, e.g. like `df[:, [:col1, :col2]]`.

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [January 25, 2021, 4:52am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/28 "2021-01-25T04:52:26Z")

</div>

I understand that I have to DataFrames.jl and CSV.jl to the newest versions, but I am limited to the cluster on which I am running.  
For my current version the  
`df[[:col1, :col2]]` or `df[:, [:col1, :col2]]` both works (I changed it to the second one though).

What I fail to do is, to push from one df that have string for columns to another df that have Int64 and Date for types.

I thought I can change the data type of column in between creating my df and pushing it into my new df\_types for the two columns.

Would pushing it into a new df work as a work around for changing the type?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 25, 2021, 5:03am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/29 "2021-01-25T05:03:54Z")

</div>

As I wrote in my answer above, you want to use `!`

```julia
df[!,:Id_internal] = tryparse.(Int64,df[:,:Id_internal])

```

Also, did you fix your initial problem of having the headers being accidentally treated as data?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 25, 2021, 5:07am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/30 "2021-01-25T05:07:51Z")

</div>

> [@wakimchris](#):
>
> What I fail to do is, to push from one df that have string for columns to another df that have Int64 and Date for types.

Why not call `parse` when you `push!`?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 25, 2021, 8:00am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/31 "2021-01-25T08:00:07Z")

</div>

> [@wakimchris](#):
>
> but I am limited to the cluster on which I am running.

You can have separate Project.toml and Manifest.toml for your project and they can have newest versions of the packages.

---

<div class="post-metadata">

**Author:** ![wakimchris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wakimchris/32/21325_2.png) [@wakimchris](https://discourse.julialang.org/u/wakimchris)\
**Post date:** [February 2, 2021, 5:25am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/32 "2021-02-02T05:25:25Z")

</div>

I found a solution:  
trying to change data type seemed a bit difficult so I changed the data type as I am pushing the items

> df\_new = DataFrame(Id\_internal=String[], Date=Date[])  
> #there is no space in between “[” and “]”

> for i in 1:length(df[:Id\_internal])  
> push!(df\_new, [(string(df[i, :Id\_internal])), (df[i, : Date])])  
> end

Thank you @pdeffebach and @bkamins

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 6, 2021, 11:45am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/33 "2021-03-06T11:45:54Z")

</div>

I wanted to convert the type of a column from Int64 to String, but none of above would work. Eg. for

`df[!,:B] = convert.(String,df[:,:B])`

I would get

> ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String

Surprisingly I found the solution on an old (2014) google forum:

`df[!,:B] = string.(df[:,:B])`

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 6, 2021, 11:55am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/34 "2021-03-06T11:55:36Z")

</div>

Other types don’t seem to work this way. So you can’t write

```julia
df[!,:B] = int64.(df[:,:B])

```

(Or `Int64`, etc)

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 6, 2021, 12:26pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/35 "2021-03-06T12:26:09Z")

</div>

So, to summarise, if I convert a column of type Int64 to one of type String, I need to use  
`string.(column)`.

If I want to convert a column of type String to one of type Float32, I need to use  
`parse.(type, column)`.

If I want to then convert the column of type Float32 to one of type Int64, then, surprisingly,  
`parse.(type,column)` will not work. Instead, I need to use  
`convert.(type,column)`.

What is going on?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 6, 2021, 3:26pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/36 "2021-03-06T15:26:30Z")

</div>

This makes sense.

- `string` turns things into strings
- `parse` turns a string into a Julia type. It only works on strings and not other types.
- `convert` converts from one Julia type to another, for things that “behave the same”. `Float32` and `Int64` are both numbers, for the most part. Whereas a `String` and an `Int64` are different things entirely.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 6, 2021, 6:04pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/37 "2021-03-06T18:04:15Z")

</div>

It should also be noted that none of this actually has anything to do with DataFrames, it’s just about how changing from one type representation to another works for certain objects everywhere in Julia.

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 7, 2021, 12:59am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/38 "2021-03-07T00:59:50Z")

</div>

This is false, actually `Int.(df[!,:B])` will convert a String array into an Int64 array.

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 7, 2021, 1:29am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/39 "2021-03-07T01:29:15Z")

</div>

Thanks @pdeffebach. One issue is that I don’t know what you mean by “Julia Type” in

> [@pdeffebach](#):
>
> `parse` turns a string into a Julia type. It only works on strings and not other types.

From a limited reading of the julialang discourse, my understanding is that `Base.parse` maps strings to numbers (whilst attempting to determine the type of number).

Also, `convert(T, x)` seems to simply be a conservative version of `T(x)` (from [Conversion and Promotion · The Julia Language](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion-vs.-Construction)).

What remains surprising is the lower case in `string(x)` vs the upper case of the type String. (`String(x)` does not work.) In contrast, `int(x)` does not work whereas `Int(x)` does. And, `Float(x)` does not work whereas `float(x)` does! I mean why should I be expected to remember the exceptional case for integers?

---

<div class="post-metadata">

**Author:** ![path-doc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/path-doc/32/19183_2.png) [@path-doc](https://discourse.julialang.org/u/path-doc)\
**Post date:** [March 7, 2021, 2:00am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/40 "2021-03-07T02:00:29Z")

</div>

In any case, it can be fixed by defining `int = Int`. Then the function `int` behaves like the functions `string` and `float`, but I was hoping the default `T` class of functions would be less quirky. (I guess my hopes were elevated by the beauty of the REPL and the syntax elsewhere in Julia.)

[Previous page](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522.md?page=1)

[Next page](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522.md?page=3)
