I'm learning to use pipelines and made a pretty simple pipeline with a FunctionTransformer
to add a new column, an ordinal encoder
and a LinearRegression
model.
But Turns out I'm getting SettingwithCopy
when I run the pipeline and I isolated the issue to the FunctionTransformer
.
Here is the Code, I omitted all the unnecessary code (like ordinal enoder and regressor in pipeline) -
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)
This gives me the follow error -
/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)
This is weird since I can do the same thing without the FunctionTransformer and not get the error.
I'm truly confused over here, so any help is appreciated