0

I have the following data frame denoting start and end dates for a specific quarter:

enter image description here

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.

1 Answer 1

0

try import pandas as pd import numpy as np

df1 = pd.DataFrame({'StartDate': pd.date_range('2016-01-01', periods=9, freq='5D'), 'EndDate': pd.date_range('2016-01-04', periods=9, freq='5D'), 'Quarter': np.arange(1, 10, 1)})

df2 = pd.DataFrame(dict(values=np.random.randn(10), date_time=pd.date_range('2016-01-01', periods=10, freq='D')))

df2['Quarter'] = np.piecewise(np.zeros(len(df2)), [(df2.date_time.values >= start_date)&(df2.date_time.values <= end_date) for start_date, end_date in zip(df1.StartDate.values, df1.EndDate.values)], df1.Quarter.values)

print df1

print df2

input data

        EndDate  Quarter  StartDate
0 2016-01-04        1 2016-01-01
1 2016-01-09        2 2016-01-06
2 2016-01-14        3 2016-01-11
3 2016-01-19        4 2016-01-16
4 2016-01-24        5 2016-01-21
5 2016-01-29        6 2016-01-26
6 2016-02-03        7 2016-01-31
7 2016-02-08        8 2016-02-05
8 2016-02-13        9 2016-02-10

output

   date_time    values  Quarter
0 2016-01-01  0.074264      1.0
1 2016-01-02  0.621282      1.0
2 2016-01-03  0.398398      1.0
3 2016-01-04 -3.435242      1.0
4 2016-01-05 -1.613446      0.0
5 2016-01-06  1.256619      2.0
6 2016-01-07  0.835417      2.0
7 2016-01-08 -0.532238      2.0
8 2016-01-09  0.047838      2.0
9 2016-01-10  0.598660      0.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.