473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jan 9 '07 #1
3 1831
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
Jan 9 '07 #2
On Jan 9, 1:57 pm, "Mike Orr" <sluggos...@gmail.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(year, 1, 1)
return jan1 + datetime.timedelta((3 - jan1.weekday()) % 7)

Jan 10 '07 #3
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,isoweek=1,isoday=1,hour=0,minute=0,s econd=0.0):

"""Week(year,isoweek=1,isoday=1,hour=0,minute=0,se cond=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:

http://www.egenix.com/files/python/mxDateTime.html#ISO

--
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.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
Jan 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: war_wheelan | last post by:
I have a db that grew 8.2GB in one week and don't understand why. There are three tables added to the db daily. I calculated the spaceused by each of the three tables for a period of two weeks. ...
13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
4
by: Neil10365 | last post by:
I wonder if someone can help me with a small conundrum I am having. This is what I want to achieve: Scenario -------- Each week, I import an excel spreadsheet called Week1.xls into an access...
3
by: Wired Hosting News | last post by:
Lets say I have 10 products in 10 different stores and every week I get a report from each store telling me how many items they have left for each of the 10 products. So each week I enter in 100...
1
by: new | last post by:
I have data for each week in a single table. I need to export this data to a separate flat file for each week. Any ideas? DB2 SQL Query export to flat files as a function of data on each record
5
by: amanda27 | last post by:
I have a database that we use in our department for the status of our projects. In the form when you pick a project from the dropdown list I have a subform that pulls the data entered for the...
5
by: amanda27 | last post by:
In my database I have a form that you go to enter project status for week ending. In that form I have a subform so that when you pick a project that it brings in the status entered for last week...
2
by: robinsiebler | last post by:
I had nothing better to do, so I thought I would make a database that contained the songs played on the internet radio station I listen to (hardradio.com) so I could see how many differents...
3
by: teneesh | last post by:
I'm trying to create two reports that produce the same data. One is a summary report, the other is detail. The detail report, for example should display all the recrods below, but combined. For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.