I have the following data frame denoting start and end dates for a specific quarter:
My function below takes an existing data set (not shown) and creates a new column called "Quarter". If the date in my existing data set falls within the start and end dates in the above dataframe, then the new "Quarter" column gets a label (i.e. Q1 or Q2). Otherwise I want it to be blank.
# dynamic function for quarterly cuts
def quarters(df, df_quarters):
for i, m in df.iterrows():
for j, (Quarter, Start_Date, End_Date) in df_quarters.iterrows():
if (m['date'] >= Start_Date) & (m['date'] <= End_Date):
df.set_value(i, 'Quarter', Quarter)
break
quarters(WSI_Hourly, df_quarters)
The function I wrote above works but with a hitch. It still labels any date prior to 2016, 1, 1 in my dataset as Q1. For example, if I have a date such as 2015, 12, 3 the Quarter column should be blank since it falls out of range. But it still labels it as Q1.
*Any help is greatly appreciated.
