Nov-19-2019, 06:37 AM
(Nov-18-2019, 08:06 AM)perfringo Wrote: My understanding has let me down. However, how should I interpret the problem:
Quote:I wanted to know if it's possible to iterate over a column set as index(DateTime /.../ so that during the iteration I can specify that the range from 00:03:00 to 23:59:00 is a day
(do something) and so on
I don't want to add a new column on time but what I'm trying to do, is to iterate on the index column of the df and do some operations in another column of that Dataframe (my Dataframe has 3 columns and I want to manipulate the df['x'] corresponding to that day range found and so on for others days that would be found!)
As I see it based on problem statement above:
Dataframe with DateTime as index. Check. Access to rows by time range. Check. Possibility to manipulate values. Check.
Hi,
I was trying to do something like this ... my bad if I express myself wrong!
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") for date in df.index.to_series().dt.date.unique(): print(date) day_value = df[df.index.to_series().dt.date == date] print(day_value) print('\n')