# DataFrames : Removing values from column

**URL:** <https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [November 8, 2021, 3:19pm UTC](https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153 "2021-11-08T15:19:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 8, 2021, 3:19pm UTC](https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153/1 "2021-11-08T15:19:42Z")

</div>

Hey everyone,

I would like to remove values that are equal to datetime2unix(DateTime(0)) from my column :

```julia
replace!(df[:,:col2], datetime2unix(DateTime(0)) => nothing)

```

OR

```julia
df[:,:col2] = replace(df[:,:col2], datetime2unix(DateTime(0)) => nothing)

```

I get this error :

```julia
MethodError: Cannot `convert` an object of type Nothing to an object of type Float64

```

Do you have any ideas ?

Thank you

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [November 8, 2021, 4:44pm UTC](https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153/2 "2021-11-08T16:44:23Z")

</div>

I can think of a few different things here:

Firstly, it sounds like you are describing a scenario where you are reading in data where you know that `DateTime(0)` isn’t actually a valid observation in the dataset. If that is indeed the case, you’re best bet is probably to handle this when you read in the dataset. Most packages have this capability as a keyword argument. For example:

```julia
using CSV
CSV.read("file.csv", missingstrings = "00:00:00") 

```

If that isn’t the issue, you could try the below:

Try doing this using an operation that is not in place. My guess is that if you ran `typeof(df[:, :col2])` you would see something like `Vector{DateTime} `. Since DateTime vectors cannot hold objects of type `Nothing`, the error makes sense. So if you just created a new object that would solve your problem:

```julia
col3 = replace(df[:, :col2], datetime2unix(DateTime(0)) => nothing)
typeof(col3)
df2 = hcat(df, col3)

```

**or**

```julia
df = transform(df, :col2 => ByRow(
      x -> if x == datetime2unix(DateTime(0))
          nothing
     else
          x
     end
) => :col2)
     

```

If you don’t need the whole row, then you could use an inplace operation to just subset out the bad observations:

```julia
filter!(:col2 => c -> c == datetime2unix(DateTime(0)), df)

```

---

<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:** [November 8, 2021, 5:21pm UTC](https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153/3 "2021-11-08T17:21:16Z")

</div>

Your issue comes from using `df[:, :col2]` indexing which attempts to use the existing column, which as Derek says can’t hold values of type `Nothing`.

You can do:

```julia
df[:,:col2] = replace(df[:,:col2], datetime2unix(DateTime(0)) => nothing)

```

or

```julia
df.col2 = replace(df[:,:col2], datetime2unix(DateTime(0)) => nothing)

```

If you create a new vector you are however re-allocating everything, which will not be the most performant way of going about this.

Note also that `nothing` is not usually meant to signify missing data, for this there is `missing`.

In summary, I would do the following:

```julia
julia> df = DataFrame(x = [now(), DateTime(0)]) # example data
2×1 DataFrame
 Row │ x
     │ DateTime
─────┼─────────────────────────
   1 │ 2021-11-08T17:17:52.754
   2 │ 0000-01-01T00:00:00

julia> allowmissing!(df, :x) # Change type of column x to allow missing values
2×1 DataFrame
 Row │ x
     │ DateTime?
─────┼─────────────────────────
   1 │ 2021-11-08T17:17:52.754
   2 │ 0000-01-01T00:00:00

julia> replace!(df.x, DateTime(0) => missing); df # use mutating version of replace
2×1 DataFrame
 Row │ x
     │ DateTime?
─────┼─────────────────────────
   1 │ 2021-11-08T17:17:52.754
   2 │ missing

```

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 9, 2021, 8:09am UTC](https://discourse.julialang.org/t/dataframes-removing-values-from-column/71153/4 "2021-11-09T08:09:56Z")

</div>

Thank you both for these answers, I replaced my values DateTime(0) with missing as you said it worked perfectly !
