我正在学习使用管道并制作了一个非常简单的管道,其中FunctionTransformer
添加了一个新列、一个ordinal encoder
和一个LinearRegression
模型。
但事实证明,SettingwithCopy
当我运行管道并将问题隔离到FunctionTransformer
.
这是代码,我省略了所有不必要的代码(如管道中的序数编码器和回归器) -
def weekfunc(df):
df['date'] = pd.to_datetime(df.loc[:,'date'])
df['weekend'] = df.loc[:, 'date'].dt.weekday
df['weekend'].replace(range(5), 0, inplace = True)
df['weekend'].replace([5,6], 1, inplace = True)
return df
get_weekend = FunctionTransformer(weekfunc)
pipe = Pipeline([
('weekend transform', get_weekend),
])
pipe.transform(X_train)
这给了我以下错误-
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:12: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
if sys.path[0] == '':
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:13: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
del sys.path[0]
/opt/conda/lib/python3.7/site-packages/pandas/core/generic.py:6619: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
return self._update_inplace(result)
这很奇怪,因为我可以在没有 FunctionTransformer 的情况下做同样的事情而不会出现错误。
我真的很困惑在这里,所以任何帮助表示赞赏