Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to split a daily DateList by week?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114914] Re: How to split a daily DateList by week?
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 23 Dec 2010 03:50:27 -0500 (EST)
  • References: <iepd94$fq3$1@smc.vnet.net>

Perhaps DateDifference could be of good use for you. DateDifference
can give differences in a number of time units including quarters and
weeks. So you could write:

toQuarter[date_List] :=
 Floor[DateDifference[{date[[1]], 1, 1, 0, 0, 0}, date,
     "Quarter"][[1]] + 1]

toWeeks[date_List] :=
 Floor[DateDifference[{date[[1]], 1, 1, 0, 0, 0}, date,
     "Week"][[1]] + 1]

I'm not sure whether your week numbers are OK this way. If you're
counting full weeks from Jan 1 this is OK. If you're counting ISO week
numbers you have to find something more complicated (see
http://en.wikipedia.org/wiki/Seven-day_week#Week_numbering).

Cheers -- Sjoerd


On Dec 21, 6:17 am, "Sohn  Hyun-U" <hs... at student.ethz.ch> wrote:
> Hi there,
>
> Suppose I have a weekdaily DateList of financial data. Sometimes, a day i=
s
> missing due to holidays etc.
> I would like to split it by quarter (Q), calendar month (M), and calendar=
 week (W), for example to compute intra-${Q,M,W} statistics.
>
> For months, I did the following:
>
> splitByMonth[ts_] := (
>   maxYear = Max[ts[[All, 1, 1]]];
>   minYear = Min[ts[[All, 1, 1]]];
>   dummy =
>    Table[Select[ts, #[[1, 1]] == year && #[[1, 2]] == month &=
], {year,
>       minYear, maxYear}, {month, 1, 12}] // Sort;
>   Flatten[dummy, {1, 2}]
>   )
>
> The problem with weeks is that there is no counter in the DateList format=
, so the only way I at the moment uses date conversions to strings of "DayN=
ame", which looks rather crummy, particularly if there are days missing som=
etimes due to holidays etc.
>
> I'm sure there is a better way but I can't find it. Can anyone here help =
me out?
>
> Kind regards,
> Hyun-U Sohn



  • Prev by Date: Export problem on complicated Animate
  • Next by Date: Using Extract where some indices are out of bounds (efficiently)
  • Previous by thread: How to split a daily DateList by week?
  • Next by thread: add-ons in objective C