# How to change the type of a column of a DataFrame

**URL:** https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074
**Category:** General Usage
**Tags:** question
**Created:** [October 19, 2019, 10:35am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074 "2019-10-19T10:35:45Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 19, 2019, 10:35am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/1 "2019-10-19T10:35:45Z")

</div>

I have a DataFrame that has a few columns of the types “Union{Missing, String}” and “Union{Missing, Float64}”. But there are no missing values in the data.

I would like to change the type of these columns to either “String” or “Float64”. How can I achieve that?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 19, 2019, 11:02am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/2 "2019-10-19T11:02:53Z")

</div>

`disallowmissing` e.g.

```julia
using Missings

a = DataFrame(a = Union{Missing, String}["a", "b"])
a[!, :a] = disallowmissing(a[!, :a])

```

The reverse of `disallowmissing` is (you guessed it!) `allowmissing`

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 19, 2019, 12:32pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/3 "2019-10-19T12:32:15Z")

</div>

Thank you for your explanation!

I couldn’t find this info in the manual: [Missing Values · The Julia Language](https://docs.julialang.org/en/v1/manual/missing/)

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 19, 2019, 12:44pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/4 "2019-10-19T12:44:09Z")

</div>

There was a long story. Anyway, these fucntions are in [https://github.com/JuliaData/Missings.jl](https://github.com/JuliaData/Missings.jl)

Perhaps they should be part of base, but it’s in an external package

---

<div class="post-metadata">

### Author: ![Rojesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rojesh/32/15390_2.png) [@Rojesh](https://discourse.julialang.org/u/Rojesh)
#### Post date: [January 1, 2021, 6:22am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/5 "2021-01-01T06:22:10Z")

</div>

In this same dataframe that allows missing  
`a = DataFrame(a = Union{Missing, String}["a", "b"])`  
if I try `push!(a,[nothing])`  
I get an error: Cannot `convert` an object of type Nothing to an object of type String  
It is a missing data, should missing allow nothing as well?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 1, 2021, 6:58am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/6 "2021-01-01T06:58:50Z")

</div>

> [@Rojesh](#):
>
> It is a missing data, should missing allow nothing as well?

Typically `nothing` should not be used to represent missing data in the statistical sense.

---

<div class="post-metadata">

### Author: ![Rojesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rojesh/32/15390_2.png) [@Rojesh](https://discourse.julialang.org/u/Rojesh)
#### Post date: [January 1, 2021, 7:23am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/7 "2021-01-01T07:23:13Z")

</div>

Thanks @Tamas_Papp  
I’m trying to push data from a JSON I got through a request which consist of nothing, I have to convert nothing in the JSON to missing before I can push it in the dataframe.  
I converted the dataframe to accept any and push it and then replaced nothing with missing.  
I was wondering if there was a better way.

---

<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 1, 2021, 7:53am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/8 "2021-01-01T07:53:17Z")

</div>

You can also do:

```julia
julia> a = DataFrame(a = Union{Missing, String}["a", "b"])
2×1 DataFrame
 Row │ a       
     │ String? 
─────┼─────────
   1 │ a
   2 │ b

julia> disallowmissing!(a, :a)
2×1 DataFrame
 Row │ a      
     │ String 
─────┼────────
   1 │ a
   2 │ bhich consist of nothing, I have to convert nothing in the JSON to missing before I can push it in the dataframe.

```

which is a bit more flexible as it allows you to pass any column selector and has `error` keyword argument.hich consist of nothing, I have to convert nothing in the JSON to missing before I can push it in the dataframe.

If you are `push!`-ing data to the data frame use `promote=true` to perform auto-promotion of column `eltype`s (although it would be better to do the conversion before pushing as @Tamas_Papp suggested).

---

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [January 1, 2021, 8:54am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/9 "2021-01-01T08:54:51Z")

</div>

I had the same problem recently. I tried to use `transform!`, but I couldn’t make it work. I ended up using `dropmissing!`.

Do you have comments regarding using these two other functions for this task?

---

<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 1, 2021, 1:26pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-column-of-a-dataframe/30074/10 "2021-01-01T13:26:16Z")

</div>

`transform!` would do it, e.g.:

```julia
transform!(df, :a => disallowmissing => :a)

```

but `dropmissing!` has `error` kwarg and it was introduced in DataFrames.jl before `transform!` was implemented.
