2
$\begingroup$

OK, here is something that is me missing the obvious.

I have an Excel spreadsheet consisting of 2 columns and 5 rows. The first column is formatted in Excel as a dd-mmm-yy date. The second column is a string.

I import the file into Mathematica:

testData = Flatten[Import["testData.xlsx","Data"],1]

enter image description here

I want to use Cases to select the rows that belong to September 3:

myDate = DateObject[{2017,9,3,0,0,0}]

enter image description here

Cases[testData,{myDate,_}]

but this gives as output:

{}

What am I missing? Thanks much in advance.

$\endgroup$
  • $\begingroup$ The proper DateObject probably has a lot of meta-info tagged onto it. Extract one of them and look at its FullForm and use the pattern you derive from that for you myDate. My guess is you'll want to do something like DateObject[{2017, 9, 3, ___}, ___] $\endgroup$ – b3m2a1 Oct 26 '17 at 19:30
  • $\begingroup$ Thank you for your help. There is indeed metadata: DateObject[{2017,9,3,0,0,0],"Instant","Gregorian",-7.`] $\endgroup$ – Suite401 Oct 26 '17 at 20:03
  • $\begingroup$ I mean myDate probably captured some of that. I dunno if it would have captured all, though. Did this pattern work: DateObject[{2017, 9, 3, ___}, ___]? $\endgroup$ – b3m2a1 Oct 26 '17 at 20:05
  • $\begingroup$ Yes I got it to work. $\endgroup$ – Suite401 Oct 26 '17 at 20:06
  • $\begingroup$ Great. Post what you did as an answer so people know this has been dealt with. $\endgroup$ – b3m2a1 Oct 26 '17 at 20:06
2
$\begingroup$

You may use DateWithinQ with a DateObject of "Day" calendar granularity within Select.

With

testData = Transpose[{DateObject@DateRange[{2017, 08, 31}, {2017, 9, 4}, "Day"], Range[5]}]

Mathematica graphics

and

targetDate = DateObject[{2017, 9, 3}, "Day"]

Mathematica graphics

Notice that targetDate is of "Day" granularity.

Then

Select[DateWithinQ[targetDate, First@#] &]@testData

Mathematica graphics

Since targetDate is of "Day" granularity any DateObject within the day (i.e. all dates of the day at any time within the day) with match in DateWithinQ.

Hope this helps.

$\endgroup$
  • $\begingroup$ The situation is reminiscent of picking out rows that contain inexact numbers with a predicate that depends on equality, where using Cases[] is clunky, and Select[] is the better choice. $\endgroup$ – J. M. will be back soon Oct 27 '17 at 2:36
1
$\begingroup$

As b3m2a1 points out, there is a lot of metadata tacked on to DateObjects, see my comment above.

Cases[testData,{DateObject[{2017,9,3,___},___],_}]

works.

$\endgroup$
  • $\begingroup$ There's a simpler solution. Use myDate = DateObject[{2017, 9, 3, 0, 0, 0.}] note the seconds value has a decimal point. With this change, your original Cases works for me. $\endgroup$ – creidhne Oct 26 '17 at 20:15
  • $\begingroup$ Yes this works too. Thank you $\endgroup$ – Suite401 Oct 26 '17 at 21:02

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.