Message Boards Message Boards

GROUPS:

String to Dates?

Posted 4 years ago
3182 Views
|
4 Replies
|
0 Total Likes
|

Trying to create a TimeSeries. The date is coming in as a string, and I cannot figure out how to do the simple conversion to a Mathematica date:

{"6/1/1990", "NA", 16.65, 0.470168, 19.4292, "NA", 0, 0.9, 0.515, 0.016121, 0.776923, "NA", 0, 0, 0.0169406, 0, -2.05909, 1, 2.22921}

I extract the dates as

t = data[[2;;, 1]]

Everything seems ok:

{"6/1/1990", "6/4/1990", "6/5/1990", "6/6/1990", "6/7/1990", "6/8/1990", "6/11/1990", "6/12/1990", ...

Tried this to convert to dates so I could create a TimeSeries:

t2 = DateList[{t, {"Month", "Day", "Year"}}]

get "cannot be interpreted as a date or time input"

This should be trivial, but I am not getting how to do it.

4 Replies

Hi, DateList function expects it's argument to be a string. It looks like you are trying to pass a list of strings to it instead. To convert a list of strings you need to map over it (since DateList is not listable by default, probably because it has a list as second argument):

test = {"6/1/1990", "6/4/1990", "6/5/1990", "6/6/1990"} ;
Map[
    DateList[{#,{"Day","Month","YearShort"}}]&,
    test
]

You can always check whether a function is listable, i.e. f[{x1,x2,..}] = {f[x1],f[x2],...}, or not:

Attributes[Sin]
Attributes[DateList]
Posted 4 years ago

Thank you Ivan, this solves my problem. I am surprised that I have to Map this as it would seem that it DateList should be vectorized, but I am learning. Any idea how I drop the H, M, S from the resulting strings? Cheers.

test = {"6/1/1990", "6/4/1990", "6/5/1990", "6/6/1990"} ;
result = Map[
    DateList[{#,{"Day","Month","YearShort"}}]&,
    test
]
result[[;;,1;;3]]
Posted 4 years ago

Thank you again.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract