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 !
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> 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
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
Is your question just about the format the date is shown in when you print the DataFrame in the REPL?
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.
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
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.
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