Alejandro Carrera
Alejandro Carrera

Reputation: 557

Issues using DateTime function with Julia

When I read a csv file in my Jupyter notebook I got the following output

54×9 Array{Any,2}:
 "\"25 Nov 2015\""  28637  11314  3804  2536  10675    4808    14122    3955  
 "\"18 Nov 2015\""  28634  11314  3804  2536  10672    4808    14122    3955  
 "\"11 Nov 2015\""  28635  11314  3805  2536  10672    4808    14122    3955  
 "\"4 Nov 2015\""   28607  11314  3810  2536  10672    4808    14089    3955  
 "\"25 Oct 2015\""  28539  11298  3806  2535  10672    4808    14061    3955  
 ⋮                                                ⋮                           
 "\"12 May 2014\""    260    182   248   171     12      11         ""      ""
 "\"1 May 2014\""     239    160   226   149     13      11         ""      ""
 "\"14 Apr 2014\""    176    110   168   108      8       2         ""      ""
 "\"31 Mar 2014\""    130     82   122    80      8       2         ""      ""
 "\"22 Mar 2014\""     49     29    49    29       ""      ""       ""      ""

I want to change the first column content into a day-month-year format in order to calculate the years passed since first observation (which in this case is my last row).

I tried converting the column using string, then using a for loop (Suppose the name of my file is "ebola"

dias = string.(ebola[:,1])

for i = 1:length(dias) 
    dias[i] = DateTime(dias[i], "d-m-y")
end

After that, I got this error message

    ArgumentError: Unable to parse date time. Expected directive DatePart(d) at char 1

I suppose this error arises because the string has a non-conventional format to manipulate it, with four "" symbols and some backslashes:

"\"25 Nov 2015\""

I must add that when i type, for example:

length("\"22 Mar 2014\"")

result is 13 when you can count 17 characters so clearly backslashes are making some "noise".

What can I don to convert the string in a proper format in order to perform the tasks I want to do? Any help will be much appreciated.

Upvotes: 1

Views: 686

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69869

The reason is that "\"22 Mar 2014\"" is a string containing " characters inside. You can check it by printing it:

julia> s = "\"22 Mar 2014\""
"\"22 Mar 2014\""

julia> print(s)
"22 Mar 2014"

In order to parse the date use a proper format string, e.g.:

julia> Date(s,"\"d u y\"")
2014-03-22

Here is how you can perform the conversion from your array:

julia> x = ["\"25 Nov 2015\""  28637  11314  3804  2536  10675    4808    14122    3955
            "\"18 Nov 2015\""  28634  11314  3804  2536  10672    4808    14122    3955
            "\"11 Nov 2015\""  28635  11314  3805  2536  10672    4808    14122    3955
            "\"4 Nov 2015\""   28607  11314  3810  2536  10672    4808    14089    3955
            "\"25 Oct 2015\""  28539  11298  3806  2535  10672    4808    14061    3955]
5×9 Array{Any,2}:
 "\"25 Nov 2015\""  28637  11314  3804  2536  10675  4808  14122  3955
 "\"18 Nov 2015\""  28634  11314  3804  2536  10672  4808  14122  3955
 "\"11 Nov 2015\""  28635  11314  3805  2536  10672  4808  14122  3955
 "\"4 Nov 2015\""   28607  11314  3810  2536  10672  4808  14089  3955
 "\"25 Oct 2015\""  28539  11298  3806  2535  10672  4808  14061  3955

julia> Date.(x[:,1], "\"d u y\"")
5-element Array{Date,1}:
 2015-11-25
 2015-11-18
 2015-11-11
 2015-11-04
 2015-10-25

Upvotes: 3

Related Questions