Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 999
Mohamed Thasin ah Avatar asked Jul 06 '20 07:07

Mohamed Thasin ah


People also ask

How do I create a time series chart in Julia?

Time series charts can be constructed from Julia either from Arrays or DataFrame columns with time like types ( DateTime or Date ). For financial applications, Plotly can also be used to create Candlestick charts and [OHLC charts], which default to date axes.

How to work with date and time in Julia?

The Dates module provides supplies classes to work with date and time. There are many functions available that are used to manipulate Date and Time in Julia. For working with Date the DateTime provides two main Modules: Date and DateTime are mostly used. They both are subtypes of the abstract TimeType.

How to convert string to number in Julia?

Following are the ways for conversion from String to Number in Julia: The string input is taken from the user using the readline () method of Julia. Next, the parse () method is used to convert the String into Integer datatype. The typeof () method outputs the datatype of the resulting integer.

How to convert different string formats to date format?

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.


Video Answer


2 Answers

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")
like image 173
Bogumił Kamiński Avatar answered Nov 03 '22 00:11

Bogumił Kamiński


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)
like image 35
Emmanuel-R8 Avatar answered Nov 03 '22 01:11

Emmanuel-R8