7

If you run this code you will see that DatePlus and DateList are inconsistent in the format of the lowest order element of the date list.

DatePlus[DateList["2012-1-1" ], {5, "Month"}] === DateList["2012-6-1"]
DatePlus[DateList["2012-1-1" ], {5, "Month"}] == DateList["2012-6-1"]
DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}]] === DateList["2012-6-1"]
DatePlus[DateList["2012-1-1" ], {5, "Month"}] // FullForm
DateList["2012-6-1"] // FullForm
DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}] ] // FullForm

This leads to issues when you try to locate elements in a list as follows:

Position[{DatePlus[DateList["2012-1-1" ], {5, "Month"}]}, DateList["2012-6-1"]]
Position[{DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}]]}, DateList["2012-6-1"]]

What is even more annoying is that I have found that wrapping a date generated by DatePlus in DateList doesn't always fix the problem and I can not tell what circumstances are required to make it work and not work.

Any insight would be appreciated. I may try to open a case with Wolfram, but my past experiences with their support team have been an unsatisfactory use of my time.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
vandel
  • 123
  • 3

2 Answers2

4

These issues revolving around casting to integer vs. float abound whenever translating data to/from external formats like Excel.

In all cases, projecting away the time information from the date proper with Take[#, 3]& or Drop[#, -3] solves your issues, but it is a hack nevertheless.

Take[#, 3] &@DatePlus[DateList["2012-1-1"], {5, "Month"}] === Take[#, 3] &@DateList["2012-6-1"]

yields True and

Position[{Take[#, 3] &@DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
 Take[#, 3] &@DateList["2012-6-1"]]

yields {{1}}

Of course, Round also works:

Position[{DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
 Round /@ DateList["2012-6-1"]]
(* {{1}} *)

If you do contact WRI again, I found that they're helpful but resource-constrained and typically promise to put your suggestion on their feature implementation queue. Perhaps referencing this link would help, as these issues are nagging in data analysis.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
alancalvitti
  • 15,143
  • 3
  • 27
  • 92
2

As you note yourself Equal handles the problem, and in fact that is typically how these cases are handled:

Position[
  {DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
  x_ /; x == DateList["2012-6-1"]
]

Position[
  {DateList[DatePlus[DateList["2012-1-1"], {5, "Month"}]]}, 
  x_ /; x == DateList["2012-6-1"]
]
{{1}}

{{1}}

If you prefer PatternTest:

Position[
  {DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
  _?(# == DateList["2012-6-1"] &)
]

Position[
  {DateList[DatePlus[DateList["2012-1-1"], {5, "Month"}]]}, 
  _?(# == DateList["2012-6-1"] &)
]
{{1}}

{{1}}

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Not sure of the etiquette here, but thanks for the responses, that's just what I needed. – vandel Jul 18 '12 at 12:05
  • @vandel you're welcome, and thanks. The etiquette is to Accept (check mark) an answer that fully satisfies the question, just as you did. – Mr.Wizard Jul 18 '12 at 20:19