# How to convert String type columns of a dataframe to a DateTime type and a Float64 type, using either plain DataFrames.jl or Query.jl

**URL:** <https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505>\
**Category:** Data\
**Created:** [January 17, 2021, 10:43pm UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505 "2021-01-17T22:43:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [January 17, 2021, 10:43pm UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505/1 "2021-01-17T22:43:53Z")

</div>

I have the following dataframe, with original columns of String type:

```julia
using DataFrames
df_0 = DataFrame(Inicio = ["10 Jan 2021 15:00 ", "10 Jan 2021 16:43 ", "10 Jan 2021 17:13 "], Q1 = ["0.50", "0.00", "0.50"])

```

I am able to convert the column named “Q1” to a Float64 type with (à la Query) the command:

```julia
using Query
df_1 = df_0 |> @mutate(Q1 = parse(Float64, _.Q1)) |> DataFrame

```

It does mutate the original df\_0 to a new df\_1 with “Inicio” remaining of String type and “Q1” now a Float64 type, as expected (is there a better or more efficient way?).

However, if then I try the naive commands below, inspired by [Standalone Query Commands · Query.jl](https://www.queryverse.org/Query.jl/stable/standalonequerycommands/#The-@mutate-command-1) , to convert the column named “Inicio” to a DateTime type with proper format( notice the spaces within the strings!):

```julia
using Dates
dtformat = DateFormat("d u Y H:M ")
df_2 = df_1 |> @mutate(Inicio = DateTime(_.Inicio, dtformat)) |> DataFrame

```

it generates another df\_2 with the columns “Inicio” and “Q1” now both of type Any (sic!). The conversion of the column Inicio to the DateTime seems to have worked, however. In fact, when I just type:

```julia
df_2

```

it displays the dataframe with the types Any (in slight gray, on the REPL), right below the two named columns, whereas when I type, e.g.:

```julia
typeof(df_2.Inicio[1]), typeof(df.Q1[1]

```

I get the wanted output tuple: (DateTime, Float64)…  
What is happening here? Shouldn’t the columns of df\_2 be of type DateTime and Float64, respectively?

---

<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 17, 2021, 11:26pm UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505/2 "2021-01-17T23:26:12Z")

</div>

In plain DataFrames.jl you would write this transformation as:

```julia
julia> transform(df_0, :Inicio => ByRow(x -> DateTime(x, DateFormat("d u Y H:M "))), :Q1 => ByRow(x -> parse(Float64, x)), renamecols=false)
3×2 DataFrame
 Row │ Inicio Q1
     │ DateTime Float64
─────┼──────────────────────────────
   1 │ 2021-01-10T15:00:00 0.5
   2 │ 2021-01-10T16:43:00 0.0
   3 │ 2021-01-10T17:13:00 0.5

```

but this is a bit verbose.

You could also just do:

```julia
julia> DataFrame(Inicio=DateTime.(df_0.Inicio, DateFormat("d u Y H:M ")), Q1=parse.(Float64, df_0.Q1))
3×2 DataFrame
 Row │ Inicio Q1
     │ DateTime Float64
─────┼──────────────────────────────
   1 │ 2021-01-10T15:00:00 0.5
   2 │ 2021-01-10T16:43:00 0.0
   3 │ 2021-01-10T17:13:00 0.5

```

which is shorter (but would create only two columns, while `transform` would keep all other columns if needed).

Also, if it is OK for you to work in-place you can just write:

```julia
df_0.Inicio = DateTime.(df_0.Inicio, DateFormat("d u Y H:M "))
df_0.Q1 = parse.(Float64, df_0.Q1))

```

which should be quite readable.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [January 18, 2021, 10:28am UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505/3 "2021-01-18T10:28:20Z")

</div>

Your last use of DataFrame’s is indeed quite readable; thank you!  
But why does my second use of Query’s @mutate does not generate columns with the “correct” types, rather than with “Any” types?

---

<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 18, 2021, 10:46am UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505/4 "2021-01-18T10:46:15Z")

</div>

Probably @davidanthoff can help you with this!

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [January 18, 2021, 10:55am UTC](https://discourse.julialang.org/t/how-to-convert-string-type-columns-of-a-dataframe-to-a-datetime-type-and-a-float64-type-using-either-plain-dataframes-jl-or-query-jl/53505/5 "2021-01-18T10:55:04Z")

</div>

Thanks for the prompt reply!
