Re: list of dates
- To: mathgroup at smc.vnet.net
- Subject: [mg88414] Re: list of dates
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 5 May 2008 06:10:29 -0400 (EDT)
On 5/3/08 at 6:20 AM, roger.nye at actuary.ch (Roger Nye) wrote: >Adapting your suggestions and after much struggle (I am new to >Mathematica), I came up with this ("date" is the list of dates >input, "t" is the list of values output): >n = Length[date]; >year = Transpose[Map[DateList, date]][[1]]; >days = Table[DateDifference[{year[[i]]}, date[[i]]], {i, 1, n}]; >t = year - 1900 + days / 365.24; >which does what I wanted (thanks). It is however very slow. It >takes a couple of minutes for 15,000 dates. All the other software I >have used for a similar calculation (Excel, Stata) does it in a >split second. Your code is doing quite a bit more than what is needed. Bob Hanlon's solution (which I like better than what I had suggested) is easily made to handle lists as follows: myDate[x_String] := DateDifference["1900-01-01", x, "Year"][[1]]; SetAttributes[myDate, Listable] In[3]:= myDate[{"1963-01-01", "1985-07-01"}] Out[3]= {63,85.4959} However, this code may not be all that much faster than your code above. The problem is DateDifference is designed to handle a fairly wide range of date formats. I believe the overhead involved to allow a wide range of date formats is the main slow down. Here is a version that gives about a factor of 2 improvement on my machine In[5]:= newDate[x_String] := DateDifference[{1900, 1, 1}, ToExpression /@ StringSplit[x, "-"], "Year"][[1]] In[6]:= SetAttributes[newDate, Listable] In[7]:= newDate[{"1963-01-01", "1985-07-01"}] // Timing Out[7]= {0.025224,{63,85.4959}} comparing: In[8]:= myDate[{"1963-01-01", "1985-07-01"}] // Timing Out[8]= {0.053429,{63,85.4959}}