# Convert date format "dd\_mm\_yyyy" in dataframe to "dd/mm/yyyy"

**URL:** <https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033>\
**Category:** General Usage\
**Tags:** dates, dataframes\
**Created:** [February 8, 2022, 3:26pm UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033 "2022-02-08T15:26:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![agatheLG](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@agatheLG](https://discourse.julialang.org/u/agatheLG)\
**Post date:** [February 8, 2022, 3:26pm UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/1 "2022-02-08T15:26:33Z")

</div>

Hi,  
I am trying to convert all the values of the columns of my dataframe which are Dates.Date type from format “dd\_mm\_yyyy” to “dd/mm/yyyy”  
I tried a lot of things like  
`Dates.format.(Date(my_dataframe[:,:Date_de_naissance],"yyyy-mm-dd"), Dates.DateFormat("dd/mm/yyyy"))` or to do the same thing in a `for i in 1:nrow(my_dataframe)` loop.  
I also tried to rewrite the date myself by doing in the same loop `new_date = "$(Dates.day(my_dataframe[i,:Date_de_naissance]))"*"/"*"$(Dates.month(my_dataframe[i,:Date_de_naissance]))"*"/"*"$(Dates.year(my_dataframe[i,:Date_de_naissance]))"`  
However none of the codes I tried seem to work…  
Would you have any idea how to proceed ?  
Thanks in advance !

---

<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:** [February 8, 2022, 3:31pm UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/2 "2022-02-08T15:31:02Z")

</div>

It looks like you are getting tripped up with broadcasting. See the below examples for sequential steps on how to do it. First, how to use `dateformat"..."` to make a format compatible with your date.

Second, how to use broadcasting to make it work.

Third, how to accomplish the task in DataFramesMeta.

```julia
julia> using Dates

julia> t = "2021_02_27"
"2021_02_27"

julia> Dates.Date(t, dateformat"yyyy_mm_dd")
2021-02-27

julia> using DataFrames

julia> df = DataFrame(t = ["2021_02_27", "2021_03_27"])
2×1 DataFrame
 Row │ t          
     │ String     
─────┼────────────
   1 │ 2021_02_27
   2 │ 2021_03_27

julia> Dates.Date.(df[:, :t], dateformat"yyyy_mm_dd")
2-element Vector{Date}:
 2021-02-27
 2021-03-27

julia> using DataFramesMeta

julia> @rtransform df :date = Dates.Date(:t, dateformat"yyyy_mm_dd")
2×2 DataFrame
 Row │ t date       
     │ String Date       
─────┼────────────────────────
   1 │ 2021_02_27 2021-02-27
   2 │ 2021_03_27 2021-03-27

```

---

<div class="post-metadata">

**Author:** ![agatheLG](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@agatheLG](https://discourse.julialang.org/u/agatheLG)\
**Post date:** [February 10, 2022, 10:23am UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/3 "2022-02-10T10:23:43Z")

</div>

Tell me if I’m wrong but I feel like what you are doing is adding a column who has a type Date instead of String.  
In my case my column is already of type Date just not under the format I want.  
I tried to add the macro @rtransform as you suggested  
`@rtransform my_dataframe :dateTest = Dates.Date(:Date_de_naissance, dateformat"dd/mm/yyyy")`  
`Date_de_naissance` being a column of `my_dataframe` with type Dates.Date under format yyyy\_mm\_dd

---

<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:** [February 10, 2022, 10:52am UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/5 "2022-02-10T10:52:52Z")

</div>

Is your question just about the format the date is shown in when you print the DataFrame in the REPL?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 10, 2022, 11:51am UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/6 "2022-02-10T11:51:20Z")

</div>

Maybe you could provide a MWE we could run and understand better?

The code below converts from Date type to String type in the desired output format.

```julia
using DataFrames, Dates
df = DataFrame(ddn = Date.(["1900_12_31", "1950_01_13", "1990_11_30"], "yyyy_mm_dd"))
df.ddn = Dates.format.(df.ddn, "dd/mm/yyyy")
df
 Row │ ddn        
     │ String
─────┼────────────
   1 │ 31/12/1900
   2 │ 13/01/1950
   3 │ 30/11/1990

```

---

<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:** [February 10, 2022, 12:13pm UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/7 "2022-02-10T12:13:52Z")

</div>

I think OP wants to have dates as `Date` type in the DataFrame, but printed differently. I think there was a discussion here about this some time ago about either wrapping it in a different type with a new `show` method or using `PrettyTables` for some custom formatting of the output, but I can’t seem to find it.

---

<div class="post-metadata">

**Author:** ![agatheLG](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@agatheLG](https://discourse.julialang.org/u/agatheLG)\
**Post date:** [February 10, 2022, 2:26pm UTC](https://discourse.julialang.org/t/convert-date-format-dd-mm-yyyy-in-dataframe-to-dd-mm-yyyy/76033/9 "2022-02-10T14:26:35Z")

</div>

> df.ddn = Dates.format.(df.ddn, “dd/mm/yyyy”)

Yeah this works thanks !  
I tried something like that but I tried it by doing :  
`my_dataframe[:,:Date_de_naissance] = Dates.format.(my_dataframe[:,:Date_de_naissance], "dd/mm/yyyy")` which was giving the error `ArgumentError: Unable to parse date time. Expected directive Delim(-) at char 3`.  
Accessing the column by doing my\_dataframe.Date\_de\_naissance instead worked for me
