Summarizing data by week

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Orr

    #1

    Summarizing data by week

    What's the best way to summarize data by week? I have a set of
    timestamped records, and I want a report with one row for each week in
    the time period, including zero rows if there are weeks with no
    activity. I was planning to use ISO weeks because datetime has a
    convenient .isocalendar() method, but I want each output row to have a
    label like this:

    2006 week 5 (Feb)

    However, to get the month (of the Thursday of that week) I have to
    convert it back to an actual date,and I don't see a method to do that
    in datetime or dateutil or mx.DateTime.

    I was planning to use a dateutil.rrule to generate the weeks from the
    minimum to maximum date, including any weeks that have no activity (so
    they won't be in the dictionary). But rrule sequences are based on an
    actual start date, not an ISO week, so that won't work. Unless perhaps
    I take the minimum date and calculate the Thursday of that week, and
    start from there. Then all my conversions would be to iso_week rather
    than from iso_week.

    Is there a better way to do this?

    --Mike

  • Larry Bates

    #2
    Re: Summarizing data by week

    Mike Orr wrote:
    What's the best way to summarize data by week? I have a set of
    timestamped records, and I want a report with one row for each week in
    the time period, including zero rows if there are weeks with no
    activity. I was planning to use ISO weeks because datetime has a
    convenient .isocalendar() method, but I want each output row to have a
    label like this:
    >
    2006 week 5 (Feb)
    >
    However, to get the month (of the Thursday of that week) I have to
    convert it back to an actual date,and I don't see a method to do that
    in datetime or dateutil or mx.DateTime.
    >
    I was planning to use a dateutil.rrule to generate the weeks from the
    minimum to maximum date, including any weeks that have no activity (so
    they won't be in the dictionary). But rrule sequences are based on an
    actual start date, not an ISO week, so that won't work. Unless perhaps
    I take the minimum date and calculate the Thursday of that week, and
    start from there. Then all my conversions would be to iso_week rather
    than from iso_week.
    >
    Is there a better way to do this?
    >
    --Mike
    >
    Generally I always check the day of week (DOW) of the beginning
    and ending dates in the sequence and adjust them so that they
    align with "real" weeks. Then it is usually as easy as adding
    7 days inside the loop to increment each row. I'm not 100% sure
    I understand the problem but perhaps this will help.

    -Larry

    Comment

    • Dan Bishop

      #3
      Re: Summarizing data by week

      On Jan 9, 1:57 pm, "Mike Orr" <sluggos...@gma il.comwrote:
      What's the best way to summarize data by week? I have a set of
      timestamped records, and I want a report with one row for each week in
      the time period, including zero rows if there are weeks with no
      activity. I was planning to use ISO weeks because datetime has a
      convenient .isocalendar() method, but I want each output row to have a
      label like this:
      >
      2006 week 5 (Feb)
      >
      However, to get the month (of the Thursday of that week) I have to
      convert it back to an actual date,and I don't see a method to do that
      in datetime or dateutil or mx.DateTime.
      >
      I was planning to use a dateutil.rrule to generate the weeks from the
      minimum to maximum date, including any weeks that have no activity (so
      they won't be in the dictionary). But rrule sequences are based on an
      actual start date, not an ISO week, so that won't work. Unless perhaps
      I take the minimum date and calculate the Thursday of that week, and
      start from there. Then all my conversions would be to iso_week rather
      than from iso_week.
      That would work. The first Thursday of the year can be computed as:

      def first_thursday( year):
      jan1 = datetime.date(y ear, 1, 1)
      return jan1 + datetime.timede lta((3 - jan1.weekday()) % 7)

      Comment

      • M.-A. Lemburg

        #4
        Re: Summarizing data by week

        On 2007-01-09 20:57, Mike Orr wrote:
        What's the best way to summarize data by week? I have a set of
        timestamped records, and I want a report with one row for each week in
        the time period, including zero rows if there are weeks with no
        activity. I was planning to use ISO weeks because datetime has a
        convenient .isocalendar() method, but I want each output row to have a
        label like this:
        >
        2006 week 5 (Feb)
        >
        However, to get the month (of the Thursday of that week) I have to
        convert it back to an actual date,and I don't see a method to do that
        in datetime or dateutil or mx.DateTime.
        You probably want to use mx.DateTime.ISO .WeekTime():

        def WeekTime(year,i soweek=1,isoday =1,hour=0,minut e=0,second=0.0) :

        """Week(year,is oweek=1,isoday= 1,hour=0,minute =0,second=0.0)

        Returns a DateTime instance pointing to the given ISO week and
        day. isoday defaults to 1, which corresponds to Monday in the
        ISO numbering. The time part is set as given.

        """

        The ISO submodule has more APIs related to ISO weeks and the
        ISO date format in general:

        Provides the most natural and robust way of dealing with date/time values in Python.


        --
        Marc-Andre Lemburg
        eGenix.com

        Professional Python Services directly from the Source (#1, Jan 12 2007)
        >>Python/Zope Consulting and Support ... http://www.egenix.com/
        >>mxODBC.Zope.D atabase.Adapter ... http://zope.egenix.com/
        >>mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
        _______________ _______________ _______________ _______________ ____________

        ::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,FreeBSD for free ! ::::

        Comment

        Working...