2

I am trying to implement Table with a DateObject with the goal to obtain a series of increasing dates:

Table[
 DateObject[Today],
 {DateRange[Today, DatePlus[Today, Quantity[5, "Days"]]]}
]

The problem is that I obtain the same DateObject instead of increasing days:

{DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.], 
 DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.], 
 DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.], 
 DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.], 
 DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.], 
 DateObject[{2020, 12, 29}, "Day", "Gregorian", 1.]}

How can I obtain a series of increasing days with Table?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Pinco
  • 31
  • 3
  • 5
    Try DateRange[Today, DatePlus[Today, Quantity[5, "Days"]]], explicit Table is not needed. – Rohit Namjoshi Dec 29 '20 at 14:52
  • thanks for your comment. Unfortunately i need Table because later in the code i need to imlement a function, which is dependent on a DateObject like this: Table[p1[DateObject[{2020,12,n}]],{n,1,20}] and i would like toimplement this automatically with a 'Today' Any idea how this can be done with Table ? – Pinco Dec 29 '20 at 15:29
  • 2
    @Pinco It would be a lot better if you described the problem you are actually trying to solve, i.e. what you mentioned in comments, rather than your attempt at its solution. I think the latter will get you off-topic answers. Would p1 /@ DateRange[Today, Today + Quantity[20, "Days"]] do? Or Table[p1[i], {i, Today, Today + Quantity[20, "Days"], Quantity[1, "Days"]}]? – MarcoB Dec 29 '20 at 15:38
  • @MarcoB thanks for your reply. Your line works fine for the function p1 which requires only one argument as DateObject, but what if you need to map to another function p2 which needs a second argument (a number) ? i tried the following: testdata1 = p2 /@ {DateRange[Today, Today + Quantity[500, "Days"]], 70.} ? i get the following error when trying on p2 which requires a second argument: PredictorFunction::mlbftlgth2: Example DateObject[{2020,12,29},Day,Gregorian,1.] should have 2 features instead of 4. do you know how to implement p2 with a second argument? – Pinco Dec 29 '20 at 16:48
  • p2[#, 70]& /@ DateRange[...]. You might want to read up on function construction a bit (e.g. Pure functions, Function (i.e. &), Slot (i.e. #), and Map (/@)). See also this FAQ on all those funny characters in MMA. – MarcoB Dec 29 '20 at 17:36
  • @MarcoB thanks again for your further reply. I implemented your suggestion (testdata1 = p2[#, 70] & /@ DateRange[Today, Today + Quantity[500, "Days"]];) but i still get an error, the following: PredictorFunction::mlbddataev: The data being evaluated is not formatted correctly. Any clue what is still wrong ? – Pinco Dec 29 '20 at 17:51
  • @Pinco No unfortunately that seems an error from PredictorFunction and it's hard to say without knowing more about your actual code – MarcoB Dec 29 '20 at 19:10
  • @MarcoB i was finally able to get the code working, the problem at this line was simply a matter of parenthesis:: testdata1 = p2[{#, 70}] & /@ DateRange[Today, Today + Quantity[365, "Days"]]; now it works. Thanks again – Pinco Dec 30 '20 at 14:14

1 Answers1

6

If you insist on Table:

Table[
  i,
 {i, Today, DatePlus[Today, Quantity[5, "Days"]], Quantity[1, "Days"]}
]

But as Rohit mentioned in their comment, you don't need Table at all. This is literally what DateRange is for:

DateRange[Today, DatePlus[Today, Quantity[5, "Days"]]]

Both produce the same result:

{DateObject[{2020, 12, 29}, "Day", "Gregorian", -6.], 
 DateObject[{2020, 12, 30}, "Day", "Gregorian", -6.], 
 DateObject[{2020, 12, 31}, "Day", "Gregorian", -6.], 
 DateObject[{2021,  1,  1}, "Day", "Gregorian", -6.], 
 DateObject[{2021,  1,  2}, "Day", "Gregorian", -6.], 
 DateObject[{2021,  1,  3}, "Day", "Gregorian", -6.]}
MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • One can even do something like Table[p[i], {i, DateRange[Today, DatePlus[Today, Quantity[5, "Days"]], Quantity[1, "Days"]]}] as an alternative to mapping a function p over the list of dates. – J. M.'s missing motivation Dec 29 '20 at 16:05
  • @J.M. Yes definitely. Indeed I had something similar in a comment, once OP explained the requirement down the road. I was also pleased to see that Today + Quantity[5, "Days"] also works, i.e. one can apparently avoid DatePlus. I find the former more natural and readable. – MarcoB Dec 29 '20 at 16:39