我有一个这样的函数,它基本上从我的数据集中转换日期(2017-08-31)并评估为类似于2012财年第一季度等:
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));
我收到错误消息
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
这似乎是导致此错误的我的函数的第一行。想检查我如何解决这个问题?对不起我对python还很新,所以想问一下。
由于
阿德里安