jeudi 10 décembre 2020

a dynamic dataframe range

I am trying to loop through a dataframe creating a dynamic ranges that are limited to the last 6 months of every row index.

Because I am looking back 6 months, I start from the first index row that has a date >= the first date in row index 0 of the dataframe. The condition which I have managed to create is shown below:

for i in df.index:

    if datetime.strptime(df['date'][i], '%Y-%m-%d %H:%M:%S') >= (datetime.strptime(df['date'].iloc[0], '%Y-%m-%d %H:%M:%S') + dateutil.relativedelta.relativedelta(months=6)): 

However, this merely creates ranges that grow in size incorporating, all data that is indexed after the first index row that has a date >= the first date in row index 0 of the dataframe.

How can I limit the condition statement to only the last 6 months of each row index?

Aucun commentaire:

Enregistrer un commentaire