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
#4
I don't know whether it addresses your problem:

import pandas as pd

data = ['2019-05-02 00:03:00', '2019-05-02 00:08:00', '2019-05-02 00:13:00', '2019-05-02 00:18:00',
        '2019-05-02 00:23:00', '2019-05-02 00:28:00', '2019-05-02 00:33:00']

df = pd.DataFrame({'num': range(len(data))}, index=pd.to_datetime(data))
It will create following DataFrame with DatetimeIndex:

Output:
num 2019-05-02 00:03:00 0 2019-05-02 00:08:00 1 2019-05-02 00:13:00 2 2019-05-02 00:18:00 3 2019-05-02 00:23:00 4 2019-05-02 00:28:00 5 2019-05-02 00:33:00 6
Now we can set num value based on time:

df.iloc[df.index.indexer_between_time('00:13:00', '23:59:00')] = 20
Which will give:

Output:
num 2019-05-02 00:03:00 0 2019-05-02 00:08:00 1 2019-05-02 00:13:00 20 2019-05-02 00:18:00 20 2019-05-02 00:23:00 20 2019-05-02 00:28:00 20 2019-05-02 00:33:00 20
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: iterate over index and define each range as a day - by perfringo - Nov-15-2019, 11:48 AM

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