Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:51:05+00:00 2026-06-07T05:51:05+00:00

Here is what I am using to group items by time frame when parsing

  • 0

Here is what I am using to group items by time frame when parsing a csv file, it is working fine, but now I would like to create slices by 4h and 30mn, this particular code just works for slices by the hour, I would like to create 4 hours slices ( or 30m slices )

tf = "%d-%b-%Y-%H"

lmb = lambda d: datetime.datetime.strptime(d["Date[G]"]+"-"+d["Time[G]"], "%d-%b-%Y-%H:%M:%S.%f").strftime(tf)

for k, g in itertools.groupby(csvReader, key = lmb):
    for i in g:
        "do something"

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:51:06+00:00Added an answer on June 7, 2026 at 5:51 am

    The general best approach is to have the groupby key return a tuple which groups items into the appropriate bucket.

    For example, for 4h slices:

    def by_4h(d):
        dt = datetime.datetime.strptime(d["Date[G]"]+"-"+d["Time[G]"], "%d-%b-%Y-%H:%M:%S.%f")
        return (dt.year, dt.month, dt.day, dt.hour // 4)
    

    You now know that if two times are in the same 4 hour slice (starting from midnight) then hour // 4 will give the same result for those times, so you end the tuple there.

    Or for 30m slices:

    def by_30m(d):
        dt = datetime.datetime.strptime(d["Date[G]"]+"-"+d["Time[G]"], "%d-%b-%Y-%H:%M:%S.%f")
        return (dt.year, dt.month, dt.day, dt.hour, dt.minute // 30)
    

    This is using // integer division for Python 3 compatibility, but it also works in Python 2.x and makes it clear that you want integer division.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery, I'm trying to group similar items in a list. Here's what I'm
I follow example from here using section listview http://lalit3686.blogspot.com/2012/05/sectionadapter.html But how can I implement
I'm trying to run NeHe's tutorial here using Python 2.7.3, but It's throwing the
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
I am attempting to follow along to this tut: HERE but using a sprite
I would like to use a GroupTemplate to separate a list of items into
I am using WaitForMultipleObject function with waitForAll parameter = true. Here using a std::vector
This question is an extension to the one raised here: Using factory_girl in Rails
EDIT: The plugin in question is located here . PHP beginner here using a
why are we using here 5 parameters for stringWithFormat : return [NSString stringWithFormat:NSLocalizedString(@LatLongFormat, @LatLongFormat),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.