# Converting columns in a DataFrame from String to Date (with a specific format)

**URL:** <https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697>\
**Category:** General Usage\
**Tags:** dates, dataframes\
**Created:** [February 18, 2022, 1:36pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697 "2022-02-18T13:36:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 18, 2022, 1:36pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/1 "2022-02-18T13:36:21Z")

</div>

I am trying to convert a DataFrame column populated with dates whose type is String and format DD/MM/YYYY to dates with Date type and format YYYYMMDD

Tried `A[!,:DATE] = parse.(Date, A[!,:DATE])`

But it errors, with: `ArgumentError: Unable to parse date time. Expected directive Delim(-)`

Which would be the correct way to achieve this conversion? Thank you!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 18, 2022, 1:44pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/2 "2022-02-18T13:44:45Z")

</div>

Parsing into a `Date` is done by:

```julia
julia> d1="02/02/2022"
"02/02/2022"

julia> date=Date(d1,"dd/mm/yyyy")
2022-02-02

julia> typeof(date)
Date

```

Output (as String) is done by:

```julia
julia> s=Dates.format(date,"Ymmdd")
"20220202"

```

So:

```julia
A[!,:DATE]= Dates.format.(Date.( A[!,:DATE],"dd/mm/yyyy"),"Ymmdd")

```

Typically, when it’s about DataFrames, after a while, much more elegant versions are popping up, just wait for it…

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [February 18, 2022, 2:59pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/3 "2022-02-18T14:59:23Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 20, 2022, 4:59pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/4 "2022-02-20T16:59:38Z")

</div>

How did you get date strings into a data frame in the first place?

In case that you use CSV.jl, you can specify a date format when reading the file [Reading · CSV.jl](https://csv.juliadata.org/stable/reading.html#dateformat)

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [February 21, 2022, 12:58am UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/5 "2022-02-21T00:58:32Z")

</div>

Don’t call `Dates.format` on your column, because it converts `Dates` to `String` so in general it is a bad practice. Leave it as `Date` unless you want to present data.

```julia
using Dates
f(x; df = dateformat"m/d/y") = Date(x, df)
transform(df, :date=>ByRow(f))

```

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 10, 2022, 12:38pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/6 "2022-10-10T12:38:55Z")

</div>

…apologies but how does this work for a column of times i.e. converting a dataframe column typically “01:10:46” string to 01:10:46 Time. I blow up with either ‘no method matching’ or ‘no method matching Int64(::Vector{Any})’ etc etc. Dataframe column is loaded with 700 rows of Any.

---

<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:** [October 10, 2022, 12:49pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/7 "2022-10-10T12:49:30Z")

</div>

The first thing to realise is that converting DataFrame columns is not different from converting regular Arrays of any type to any other type, as DataFrame columns are just vectors. So you really just need to know how to convert a `String` to a `Time` object, irrespective of where this `String` is stored.

The second thing to note is that getting a numerical value out of a string is generally referred to as “parsing”, rather than “conversion”. With this, you have:

```julia
julia> using Dates

julia> parse(Time, "01:10:46")
01:10:46

julia> typeof(ans)
Time

```

and then broadcast that over your data, i.e. `parse.(Time, df.timecol)` (although I can’t guarantee that all your strings have the right format to be parsed, so you might have to fiddle with it a bit!)

PS how did you end up with 700 columns of type `Any`? If you are using XLSX.jl to read an Excel file, consider the `infer_eltypes = true` kwarg.

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 10, 2022, 1:15pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/8 "2022-10-10T13:15:33Z")

</div>

thank you very much for the reply. I have a df column with 700 rows of string time in a df.Time column which has come from an xslx but I had missed the infer\_eltypes option.

parse(Time, df.“Time”) gives methodError

I’m trying to re-write the entire df.Time column. In the xlsx file the times are in time format and add up.

new to Julia and coming from a Python background. Again thanks for the comment, I’ll keep at it.

---

<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:** [October 10, 2022, 1:27pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/9 "2022-10-10T13:27:49Z")

</div>

My suggestion was `parse.(Time, df."Time")` - note the dot after `parse` to broadcast (apply element-wise) the function.

(also note the quotes aren’t necessary if you have a column name which is a single word without special characters)

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 10, 2022, 1:39pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/10 "2022-10-10T13:39:17Z")

</div>

![Screenshot from 2022-10-10 14-37-13](https://global.discourse-cdn.com/julialang/original/3X/f/e/fed450cef4ccf69648c9c35cff124dc8c3db5aef.jpeg)  
Tried the broadcast (sorry) my mis-typing.

julia\> parse.(Time, df.Time)  
ERROR: MethodError: no method matching parse(::Type{Time}, ::Time)

---

<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:** [October 10, 2022, 1:42pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/11 "2022-10-10T13:42:24Z")

</div>

You have already converted the `df.Time` column to a `Time` type. It is no longer a string, so `parse` doesn’t work. Try again on your “raw” data frame with string types.

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 10, 2022, 1:49pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/12 "2022-10-10T13:49:18Z")

</div>

Good spot 🙂 the df.Time column got corrected with the great infer\_eltype suggestion. Other similar columns in the df remained Any and a parse.(Time, df.“Avg Pace”) gives…

 ![Screenshot from 2022-10-10 14-44-53](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffbf91f21283af911502d63b532e3e359d7c9d0d.jpeg)

---

<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:** [October 10, 2022, 1:51pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/13 "2022-10-10T13:51:10Z")

</div>

The problem is the same, as before. Your column isn’t all `String`s and `parse` only works on strings.

How are you importing your data? It’s not a great sign to have these `Any` columns.

---

<div class="post-metadata">

**Author:** ![StuartRL](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@StuartRL](https://discourse.julialang.org/u/StuartRL)\
**Post date:** [October 10, 2022, 2:11pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/14 "2022-10-10T14:11:18Z")

</div>

Interesting, perhaps I go back a raw CSV.File input, which I moved away from to

DataFrame(XLSX.readtable(“file.xlsx”, “Activities”, infer\_eltypes = true))

with the useful suggestion of infer\_eltypes that worked on the first Time column leaving the remaining others.

Think I’ll take a few steps backwards. Didn’t want to take up too much of people’s time etc. From the comments I think its the raw data more than my logic which is a step. Thanks.

---

<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:** [October 10, 2022, 2:15pm UTC](https://discourse.julialang.org/t/converting-columns-in-a-dataframe-from-string-to-date-with-a-specific-format/76697/15 "2022-10-10T14:15:32Z")

</div>

Don’t worry! These are annoying problems to run into.

One solution would be a helper function

```julia
get_to_time(x::String) = parse(Time, x)
get_to_time(x::Time) = x
get_to_time(x) = missing
get_to_time.(df.Time)

```
