Python Forum
iterate over index and define each range as a day
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterate over index and define each range as a day
#5
Previously I wrote a different answer and saw too late, that it was already answered.
If you need something, which generates datetime intervals e.g. for business days:

Here an example with dateutil.
You should read the documentation about dateutil.rrule.

It can generate intervals.
Here an example how to generate intervals with dateutil package.
Maybe it's useful together with pandas, but also without pandas.


import datetime as dt

# python3 -m pip install dateultil --user
# or install it in a venv
# often this package is installed, because other packages depends on this module

from dateutil.rrule import rrule, HOURLY


def hourly(start, interval, count):
    """
    Finite generator for hourly intervals
    count defines how many elements
    """
    for dt_interval in rrule(freq=HOURLY, interval=interval, dtstart=start, count=count):
        yield dt_interval
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: iterate over index and define each range as a day - by DeaD_EyE - Nov-15-2019, 07:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Index out of range error standenman 0 1,092 May-22-2023, 10:35 PM
Last Post: standenman
  [split] Getting Index Error - list index out of range krishna 2 2,606 Jan-09-2021, 08:29 AM
Last Post: buran
  Cycle through Numpy range within another range(?) Zero01 0 2,015 Jul-31-2020, 02:37 PM
Last Post: Zero01
  Getting Index Error - list index out of range RahulSingh 2 6,145 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,578 Jan-28-2020, 09:30 AM
Last Post: axelle
  IndexEroor : index out of range adithyakrish 7 6,819 May-19-2017, 07:34 PM
Last Post: buran
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 19,174 Dec-12-2016, 10:51 PM
Last Post: m_lotinga
  Unable to understand reason for error IndexError: tuple index out of range rajat2504 4 54,122 Dec-09-2016, 11:04 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020