Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

How to convert String Series to Datetime Series in Julia

I have a csv file which looks like below,

20×2 DataFrame
│ Row │ Id    │ Date       │
│     │ Int64 │ String     │
├─────┼───────┼────────────┤
│ 1   │ 1     │ 01-01-2010 │
│ 2   │ 2     │ 02-01-2010 │
│ 3   │ 3     │ 03-01-2010 │
│ 4   │ 4     │ 04-01-2010 │
│ 5   │ 5     │ 05-01-2010 │
│ 6   │ 6     │ 06-01-2010 │
│ 7   │ 7     │ 07-01-2010 │
│ 8   │ 8     │ 08-01-2010 │
│ 9   │ 9     │ 09-01-2010 │
│ 10  │ 10    │ 10-01-2010 │
│ 11  │ 11    │ 11-01-2010 │
│ 12  │ 12    │ 12-01-2010 │
│ 13  │ 13    │ 13-01-2010 │
│ 14  │ 14    │ 14-01-2010 │
│ 15  │ 15    │ 15-01-2010 │
│ 16  │ 16    │ 16-01-2010 │
│ 17  │ 17    │ 17-01-2010 │
│ 18  │ 18    │ 18-01-2010 │
│ 19  │ 19    │ 19-01-2010 │
│ 20  │ 20    │ 20-01-2010 │

after reading the csv file date columns is in String type. How to externally convert a string series into Datetime series. In Julia Data Frame docs doesn't talk Anything about TimeSeries. How to externally convert a series or vector into Datetime format? Is there anyway I can mention timeseries columns while reading a CSV File?

Upvotes: 6

Views: 4665

Answers (2)

Emmanuel-R8
Emmanuel-R8

Reputation: 31

Here is how I have done it:

First a helper function to convert different string formats.

parse_date(d::AbstractString) = DateTime(d, dateformat"yyyy-mm-dd HH:MM:SS")
parse_date(v::Vector{AbstractString}) = parse_date.(v)
parse_date(v::Vector{String}) = parse_date.(v)
parse_date(v::Vector{String31}) = parse_date(String.(v))
using Pipe, TimeSeries

prices = @pipe CSV.File(filename; header = 1, delim = ",") |>
         TimeArray(_; timestamp = :Date, timeparser = parse_date)

Upvotes: 1

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69819

When reading-in a CSV file you can specify dateformat kwarg in CSV.jl:

CSV.File("your_file_name.csv", dateformat="dd-mm-yyyy") |> DataFrame

On the other hand if your data frame is called df then to convert String to Date in your case use:

using Dates
df.Date = Date.(df.Date, "dd-mm-yyyy")

Upvotes: 11

Related Questions