Message Boards Message Boards

0
|
1391 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Conversion of list to DateObject

Posted 1 year ago

I am working with this dataset with a date format similar to input below, how do I apply date object to the list such that the first 4 digits are extracted and converted to the date object similar to the output below.

 input = {
 {"date_start"},
 {19900201},
 {19900201},
 {19980201},
 {20010201}
}

output = {date_start, 1990, 1990, 1998, 2001}
POSTED BY: Tobi Babatunde
3 Replies
Posted 1 year ago
input = {{"date_start"}, {19900201}, {19900201}, {19980201}, {20010201}};
expectedResult = {"date_start", 1990, 1990, 1998, 2001};

One very literal approach would be:

result = MapAt[Quotient[#, 10000] &, Flatten@input, 2 ;;]

But you mentioned "dateobject". Are you actually wanting DateObjects?

MapAt[DateObject[{Quotient[#, 10000], Quotient[Mod[#, 10000], 100], Mod[#, 100]}] &, Flatten@input, 2 ;;]
POSTED BY: Eric Rimbey

Both options are actually helpful considering that I am trying to use the complete data for time series and other analysis as well, thanks!

POSTED BY: Tobi Babatunde

Another way using string matching

input // Flatten //
 MapAt[ToString/*(DateObject[{#, {"Year", "", "Month", "", "Day"}}, "Day"] &), 2 ;;]
POSTED BY: Rohit Namjoshi
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