I have a column called maturity_dt
filled with datetime
objects in my dataframe df
, and I am just trying to select only the rows in the column which have a maturity_dt
in August or February. So, I am trying to delete all the rows that do not correspond with these months dynamically using the code below. However, I get the error IndexError: index 109235 is out of bounds for axis 0 with size 44681
despite using reset_index
, so I am wondering if there is another way to delete rows dynamically.
for (i, row) in df.iterrows():
dateold = datetime.datetime.strptime(row['maturity_dt'], '%Y-%m-%d %H:%M:%S.%f')
datenew = dateold.date()
date = str(datenew).split('.')[0]
h,m,s = re.split('-', date)
if m != 2 and m != 8: # If the month is not Feb or August
df.drop(df.index[i])
df = df.reset_index(drop=True)
Thank You