Skip to content Skip to sidebar Skip to footer

Iterate Over Index And Define Each Range As A Day

my last question didn't find any help/answer and I found another approach and I wanted to know if it's possible to iterate over a column set as index(DateTime with pandas format:

Solution 1:

You can do a loop on day of datetime like this.

import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(columns=['SomeDatetime'], index=[0,1,2,3,4,5])
now = datetime.now()
df.loc[0, 'SomeDatetime'] = now + timedelta(minutes = 10)
df.loc[1, 'SomeDatetime'] = now - timedelta(days = 1)
df.loc[2, 'SomeDatetime'] = now + timedelta(minutes = 15)
df.loc[3, 'SomeDatetime'] = now + timedelta(minutes = 20)
df.loc[4, 'SomeDatetime'] = now + timedelta(minutes = 50)
df.loc[5, 'SomeDatetime'] = now + timedelta(minutes = 90)

print("Dataframe\n")
print(df)
print("\nDay Loop\n")
for day indf['SomeDatetime'].dt.day.unique():
    day_value = df[df['SomeDatetime'].dt.day == day]
    print(day_value)

This gives you the following result:

DataframeSomeDatetime02019-11-15 09:02:47.78475212019-11-14 08:52:47.78475222019-11-15 09:07:47.78475232019-11-15 09:12:47.78475242019-11-15 09:42:47.78475252019-11-15 10:22:47.784752DayLoopSomeDatetime02019-11-15 09:02:47.78475222019-11-15 09:07:47.78475232019-11-15 09:12:47.78475242019-11-15 09:42:47.78475252019-11-15 10:22:47.784752SomeDatetime12019-11-14 08:52:47.784752

You can do this for week and others as well. Edit after comment

import numpy as np
import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(columns=['SomeDatetime', 'x', 'y'], index=[0,1,2,3,4,5])
now = datetime.now()
df.loc[0, 'SomeDatetime'] = now + timedelta(minutes = 10)
df.loc[1, 'SomeDatetime'] = now - timedelta(days = 1)
df.loc[2, 'SomeDatetime'] = now + timedelta(minutes = 15)
df.loc[3, 'SomeDatetime'] = now + timedelta(minutes = 20)
df.loc[4, 'SomeDatetime'] = now + timedelta(minutes = 50)
df.loc[5, 'SomeDatetime'] = now - timedelta(days = 30*4)  - timedelta(days = 3)

df['x'] = pd.Series(np.random.randn(6))
df['y'] = pd.Series(np.random.randn(6))

df.set_index('SomeDatetime', inplace=True)

print("Dataframe\n")
print(df)
print("\nDay Loop\n")

fordatein df.index.to_series().dt.date.unique():
    print(date)
    day_value = df[df.index.to_series().dt.date == date]
    print(day_value)
    print('\n')

Result:

DataframexySomeDatetime2019-11-15 13:38:13.885939 -1.1067881.4967312019-11-14 13:28:13.885939 -0.478087-0.8193662019-11-15 13:43:13.885939  1.9853280.8414952019-11-15 13:48:13.885939  0.915070-0.0654732019-11-15 14:18:13.885939 -0.7267030.4048962019-07-15 13:28:13.885939  0.669062-0.543737DayLoop2019-11-15xySomeDatetime2019-11-15 13:38:13.885939 -1.1067881.4967312019-11-15 13:43:13.885939  1.9853280.8414952019-11-15 13:48:13.885939  0.915070-0.0654732019-11-15 14:18:13.885939 -0.7267030.4048962019-11-14xySomeDatetime2019-11-14 13:28:13.885939 -0.478087-0.8193662019-07-15xySomeDatetime2019-07-15 13:28:13.885939  0.669062-0.543737

Does this help?

Post a Comment for "Iterate Over Index And Define Each Range As A Day"