3
$\begingroup$

enter image description hereI am new to Mathematica and I would like to select data from a database using dates as criteria. In this moment I am working on the ResourceData["New Orleans Slave Sales 1856-1861"] from the Wolfram Data Repository. I would like to select from the database only the rows in which the slaves were sold from 1858 till the end. I already tried with the function Select and DateRange

mydata = ResourceData["New Orleans Slave Sales 1856-1861"];    
Select[mydata,DateRange[DateObject[{1,1,1859}],DateObject[{1,1,1862}]]]

but apparently it does not work, or I am using it wrong. Could someone help me in debugging it or suggest me a better one?

$\endgroup$
4
  • $\begingroup$ the second argument of Select needs to evaluate to True or False; perhaps you could try Select[mydata,MemberQ[DateRange[DateObject[{1,1,1859}],DateObject[{1,1,1862}]],#]&] $\endgroup$
    – user42582
    Commented Feb 24, 2018 at 13:09
  • $\begingroup$ Unfortunately it seems to not make things better :/ $\endgroup$ Commented Feb 24, 2018 at 13:16
  • $\begingroup$ how about dates=DateRange[DateObject[{1,1,1859}],DateObject[{1,1,1862}]]; mydata[Select[MemberQ[dates,#"SalesDate"]&]]? $\endgroup$
    – user42582
    Commented Feb 24, 2018 at 14:23
  • $\begingroup$ Nothing more than the white square.. $\endgroup$ Commented Feb 24, 2018 at 14:28

3 Answers 3

7
$\begingroup$

Please try:

dates = DateRange[DateObject[{1859, 1, 1}], DateObject[{1862, 1, 1}]];
mydata[Select[MemberQ[dates, #"SalesDate"] &]]

The date format entered for WL is {yyyy,m,d} you were querying for a date that does not exists in the dataset

$\endgroup$
2
  • $\begingroup$ It does work! Thank you very much :) $\endgroup$ Commented Feb 24, 2018 at 23:04
  • $\begingroup$ that is great! I just realised however that when dealing with DateObjects with 'seconds' precision, it is important to check that your imported dates havethe same level of decimal precision. Otherwise it returns an empty dataset. Your method really fast as well! $\endgroup$
    – alex
    Commented Mar 23, 2021 at 14:29
0
$\begingroup$

Although @Hans has provided the best answer so far, I'd like to show an alternative just in case people are interested. It is significantly slower for this particular case, but it works just as well:

mydata[Select[DateObject[{1859, 1, 1}] <= #"SalesDate" <=  DateObject[{1862, 1, 1}]&]]

It is simpler to the eye and probably has similar speed capabilities as the MemberQ when it comes to simpler structures other than DateObjects[].

EDIT: There is a minor chance where generating your own date range might conflict with the DateObjects that you have imported from a file or website. For example if your granularity is down to the second, then it is possible that the MemberQ method that @Hans shows will not recognise.

If you want to go with @Hans' method, I would recommend to extract the data directly from your dataset using DateSelect:

DateSelect["your_dates",1859 <= #Year<= 1862 && #Month ==1 && #Day==1 &]

$\endgroup$
0
$\begingroup$

I prefer Alex's answer. It seems that Han's answer requires the date list to have the same format as the dates you are comparing in your dataset. e.g. if dataset is in mmm/yy format, but dates list is in GMT default format, the memberQ will fail. A direct comparison based on the date's value is more intuitive in my opinion. Thanks @alex!

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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