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?
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.
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.
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.
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.
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")
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With