0

I have a function like this that basically transforms takes in date (2017-08-31) from my data set and evaluates to like FY 17 Q1 etc.:

def quarter_classification(df):
    df['Line End Date Year'],df['Line End Date Month'] = df['Line End Date'].dt.year , df['Line End Date'].dt.month;
    df['Quarter'] = np.where((df['Line End Date Month'] >=3) & (df['Line End Date Month'] <=5),'Q4',np.where((df['Line End Date Month'] >=6) & (df['Line End Date Month'] <=8),'Q1',np.where((df['Line End Date Month'] >=9) & (df['Line End Date Month'] <=11),'Q2','Q3')));
    df['Line End Date Fiscal Quarter'] = np.where((df['Quarter'] =='Q2') |(df['Quarter'] =='Q1')|(df['Line End Date Month'] ==12),df['Line End Date Year']+1,df['Line End Date Year']);
    df['Fiscal Year'] = np.where((df['Quarter'] =='Q2') |(df['Quarter'] =='Q1')|(df['Line End Date Month'] ==12),df['Line End Date Year']+1,df['Line End Date Year']);
    df['Line End Date Fiscal Quarter'] = df['Line End Date Fiscal Quarter'].astype(str);
    df['Line End Date Fiscal Quarter'] = df['Line End Date Fiscal Quarter'].str.slice(2, 4);
    df['Line End Date Fiscal Quarter'] = 'FY' + df['Line End Date Fiscal Quarter'] + ' ' + df['Quarter'];
    return (df);

print(quarter_classification(quarter_df_temp));

I am getting a error which says

Automation.py:39: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  df['Line End Date Year'],df['Line End Date Month'] = df['Line End Date'].dt.year , df['Line End Date'].dt.month;
Automation.py:40: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  df['Quarter'] = np.where((df['Line End Date Month'] >=3) & (df['Line End Date Month'] <=5),'Q4',np.where((df['Line End Date Month'] >=6) & (df['Line End Date Month'] <=8),'Q1',np.where((df['Line End Date Month'] >=9) & (df['Line End Date Month'] <=11),'Q2','Q3')));
Automation.py:41: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

It seems to be the first line of my function that causing this error. Wanted to check how do i resolve this? Sorry I am still very new to python so wanted to ask.

Thanks

Adrian

Adam
  • 1,157
  • 6
  • 19
  • 40

0 Answers0