Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fill subsequent values of python pandas dataframe with zeros when a condition is met

When a value in "Returns" column is less than "m"(say m=-0.5), I want the subsequent rows to be filled with zeros only till that month end. How to accomplish this?
Thanks in advance.

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

date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'Price': data})
df = df.set_index('test')
df['Returns']=df['Price'].pct_change()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use numpy.where with mask for compare Returns for less like m and aggregate Series.cummax with shift for < m per months by DatetimeIndex.to_period:

np.random.seed(123)
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'Price': data})
df = df.set_index('test')
df['Returns']=df['Price'].pct_change()
    
    
m = -0.5

m = (df['Returns'].lt(m)
                  .groupby(df.index.to_period('m'))
                  .transform(lambda x: x.shift().cummax())
                  .fillna(False))

df['Returns'] = np.where(m, 0, df['Returns'])
print (df)
                            Price   Returns
test                                       
2021-11-22 14:32:35.550767     29       NaN
2021-11-23 14:32:35.550767     56  0.931034
2021-11-24 14:32:35.550767     82  0.464286
2021-11-25 14:32:35.550767     13 -0.841463
2021-11-26 14:32:35.550767     35  0.000000
                          ...       ...
2022-11-18 14:32:35.550767     47  0.000000
2022-11-19 14:32:35.550767     90  0.000000
2022-11-20 14:32:35.550767     20  0.000000
2022-11-21 14:32:35.550767     27  0.000000
2022-11-22 14:32:35.550767     51  0.000000

[366 rows x 2 columns]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading