# 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:** 1

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 4, 2020, 3:03pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/1 "2020-03-04T15:03:29Z")

</div>

Was looking around, but didn’t find an answer.

Specifically, I have a `DataFrame` and one column has data type `Int64`; it has the unique values `0` and `1` (meaning obviously `false` and `true`). What is the easiest quickest way to have the column including its entries converted to the boolean data type?

I thought about a kind of loop, but are there already existing functions available that I should know of?

More generally, please explain how to best tackle this (especially conceptually), if using a self-made solution.

My thought then might be to take the whole array/column, check every value, make a new array based on set conditions (if 0, make false; if 1, make true, etc.), mutate or add the new array into the dataframe.

---

<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 4, 2020, 3:13pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/2 "2020-03-04T15:13:33Z")

</div>

`df.int_col .== 1` will return a `BitArray` column

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 4, 2020, 3:20pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/3 "2020-03-04T15:20:25Z")

</div>

Very useful. Thanks.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 4, 2020, 4:47pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/4 "2020-03-04T16:47:05Z")

</div>

A more general way to do this is (assuming the column is called `x`)

```julia
df[!,:x] = convert.(Bool,df[!,:x])

```

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 5, 2020, 10:51am UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/5 "2020-03-05T10:51:15Z")

</div>

Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 13, 2020, 3:57pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/6 "2020-03-13T15:57:27Z")

</div>

I suppose I can post this here, since it concerns a similar issue.

There is a dataframe. It has a `String` column with `missing` values. Its values are actually integers.

What is the most direct and easiest way to convert this whole column of `String` (with `missing`) to one of `Int64` (with `missing`)?

I thought of your generic way, @tbeason, but it seems it requires more in this case. I thought there was a function to convert such strings to int, but I could be mistaken.

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 13, 2020, 4:05pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/7 "2020-03-13T16:05:31Z")

</div>

I’m getting some progress. I found the function `parse()`.

---

<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 13, 2020, 5:07pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/8 "2020-03-13T17:07:38Z")

</div>

Unfortunately `parse` doesn’t work with `missing`s. You are looking for `passmissing` from Missings.jl.

```julia
julia> df.col = passmissing(parse).(Int, df.col)

```

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [March 14, 2020, 12:55pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/9 "2020-03-14T12:55:33Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![kailukowiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kailukowiak/32/9878_2.png) [@kailukowiak](https://discourse.julialang.org/u/kailukowiak)\
**Post date:** [May 26, 2020, 10:51pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/10 "2020-05-26T22:51:22Z")

</div>

I believe that you could just have

```julia
df[:,:x] = convert.(Bool,df[!,:x])

```

(notice that `:` instead of `!`) to avoid making two copies.

---

<div class="post-metadata">

**Author:** ![kailukowiak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kailukowiak/32/9878_2.png) [@kailukowiak](https://discourse.julialang.org/u/kailukowiak)\
**Post date:** [July 29, 2020, 5:47pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/11 "2020-07-29T17:47:14Z")

</div>

My mistake, it has to be the other way around:

```julia
df[!,:x] = convert.(Bool,df[:,:x])

```

---

<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 23, 2021, 7:50pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/12 "2021-01-23T19:50:35Z")

</div>

When I do  
`df[!,:x] = convert.(Int64,df[:,:x])`  
I get

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

How to get this resolved ?

---

<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 23, 2021, 7:53pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/13 "2021-01-23T19:53:33Z")

</div>

You want `parse.(Int64, df[:, :x])`

---

<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, 6:22pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/14 "2021-01-24T18:22:47Z")

</div>

My column is called  
`Id` set as String, need to change it to Int64  
and my column have 10 digit codes as entries

`df[!,:Id] = parse.(Int64,df[:,:Id])`

I get  
`ERROR: ArgumentError: invalid base 10 digit 'I' in "Id"`

---

<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, 6:32pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/15 "2021-01-24T18:32:27Z")

</div>

Could you copy and paste par of your column into this thread? You probably want `tryparse` which will return `nothing` if `parse` finds a column like `"Id209.4"`, which can’t be parsed as a float.

---

<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, 6:41pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/16 "2021-01-24T18:41:09Z")

</div>

my cloumns are:

```julia
Id_internal | Date
7483947898 | 2020-11-28
7475629104 | 2021-01-23
7384881913 | 2020-12-28

```

Both columns are set as integers but I would want to push the first into an `Int64` and the second into a `Date`

---

<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, 7:46pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/17 "2021-01-24T19:46:51Z")

</div>

Your problem is that the first row of your data frame is `"Id_internal"` and `"Date"`.

How are you reading in your data? Perhaps you can change it so that your data doesn’t accidentally include the names of your variables.

Did you do my `tryparse` idea? That should fix it in the meantime. You can also do

```julia
df = df[2:end, :]

```

to get rid of the first row.

---

<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, 8:44pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/18 "2021-01-24T20:44:45Z")

</div>

> [@pdeffebach](#):
>
> tryparse

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

both gave me  
`ERROR: setindex! not defined for WeakRefStrings.StringArray{String,1}`

> [@pdeffebach](#):
>
> How are you reading in your data?

I read my data as follow:  
`df_all = CSV.File("file.csv", delim = '\t' |> DataFrame`  
I then I create a df with what I need  
`df = df_all[[:Id_internal, :Date]]`

---

<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:44pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/19 "2021-01-24T21:44:18Z")

</div>

What is the version of CSV.jl and DataFrames.jl you are using?

---

<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, 9:44pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/20 "2021-01-24T21:44:29Z")

</div>

That is indeed a very odd error message. To be honest I don’t know _exactly_ why you are getting it. But note that you should be writing `df[:, :Id_internal]`, _not_ `df[:2, :Id_internal]`

cc @quinnj for why the user might have gotten such an odd error. I can’t replicate it.

> [@wakimchris](#):
>
> df = df\_all[[:Id\_internal, :Date]]

This is old, deprecated, syntax. Its a concern that people are still finding this syntax in tutorials. Can you please post a link to the guide you are using to learn DataFrames?

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