Message Boards Message Boards

0
|
3917 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

List manipulation with DateObject

I work with a list containing DateObjects. I want to transform the dateobject to a "Month". The list contains 6 parts.

Fot simplicity, the list looks like:

test = {{"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 21.95}};

The transformation rule is like:

test /. {a_, b_, c_, d_, e_, f_} -> {a, b, DateObject[c, "Month"], d, e, f};

The output is:

{ {"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95} , DateObject[{"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95}, "Month"] , {"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95} , {"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95}}

It goes wrong with the third element of the list.

DateObject[{"ASX", 15, DateObject[{2021, 3, 8}], "buy", "F", 21.95}, "Month"]

When the original list contains 5 elements or 7 elements, the output is correct. When the list contains the same number of elements as the parameters a, b, ... f_ then something goes wrong.

For example:

test5 = {{"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}};
test5 /. {a_, b_, c_, d_, e_, f_} -> {a, b, DateObject[c, "Month"], d, e, f}

test7 = {{"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}, {"ASX", 15, DateObject[{2021, 03, 08}], "buy", "F", 
    21.95}};

test7 /. {a_, b_, c_, d_, e_, f_} -> {a, b, DateObject[c, "Month"], d, e, f}

The transformations with 5 and 7 elements in de list are correct. When the list containts 6 elements, it's goes wrong.

When I use a different a transformation like

Map[ {#[[1]], #[[2]], DateObject[#[[3]], "Month"], #[[4]], #[[5]], #[[6]]} &, test]

Then I don't have any problem.

I would like to understand what's wrong with the first transformation, and why it goes wrong when there is a relation with the number of elements in the list. Anyone a suggestion?

POSTED BY: Michiel van Mens
2 Replies
Posted 3 years ago

The problem is that each nested list also contains 6 items so the c matches the third sublist.

l6 = Range[36] // Partition[#, 6] &
l6 /. {a_, b_, c_, d_, e_, f_} :> c
(* {13, 14, 15, 16, 17, 18} *)

l5 = Most@l6
l5 /. {a_, b_, c_, d_, e_, f_} :> c
(* {3, 9, 15, 21, 27} *)

To avoid issues like this use more specific patterns

test /. {a_, b_, c_DateObject, d_, e_, f_} :> {a, b, DateObject[c, "Month"], d, e, f}
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Crossposted here.

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