1
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ MapAt[DateObject[#, "Month"] &, test, {All, 3}] ? $\endgroup$
    – Ben Izd
    Jun 23 at 10:11
  • $\begingroup$ I don't know why it behaves that way. If you really wanna use replacement rule, you can use operator form of ReplaceAll as ReplaceAll[{a_, b_, c_, d_, e_, f_} -> {a, b, DateObject[c, "Month"], d, e, f}] /@ test $\endgroup$ Jun 24 at 2:55
  • $\begingroup$ Crossposted here. $\endgroup$ Jun 24 at 14:25
  • 1
    $\begingroup$ The issue with 6 elements is that the pattern suddenly matches the entire list (not only its elements). Therefore it treats the third list element as the date object, and tries to extract the month from it $\endgroup$
    – Lukas Lang
    Jul 23 at 8:13
  • $\begingroup$ you can also do test /. {a_, b_, c_DateObject, d_, e_, f_} :> {a, b, DateObject[#, "Month"] & @@ c, d, e, f}. $\endgroup$
    – kglr
    Jul 23 at 21:51
0
$\begingroup$

I think you need 2nd level operation, because you have list of list. This works.

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

You can also use this.

test[[All, 3]] = DateObject[#, "Month"] & /@ test[[All, 3]]
$\endgroup$
4
  • $\begingroup$ Thanks. And do you have a explanation why it's goes wrong with 6 elements in the list en not with 5 or 7 elements? $\endgroup$ Jun 22 at 15:08
  • $\begingroup$ Can you edit your question with a list 5 elements and 7 elements? $\endgroup$ Jun 22 at 17:53
  • $\begingroup$ I updated the question with new examples $\endgroup$ Jun 23 at 7:12
  • $\begingroup$ Level 1 is enough for Replace (the default is {0}), 2 will try to replace the individual parts of the list items $\endgroup$
    – Lukas Lang
    Jul 23 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.