I'm working with Python and Pandas, and I ran into this warning. My code still works, although I don't think ignoring this warning is good practice. I have a dataframe of a csv file that i'm working with. I'm trying to take only two of the many columns this csv has, 'timestamp' and 'cost'. Im getting the following warning:
test.py:16: 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/user_guide/indexing.html#returning-a-view-versus-a-copy
df['timestamp'] = pd.to_datetime(df['timestamp'])
My code:
df = df[['timestamp', 'cost']]
df['timestamp'] = pd.to_datetime(df['timestamp']) // this is where the error is occuring
mask = (df['timestamp'] >= start) & (df['timestamp'] <= end)
df = df.loc[mask].sum()
I've tried reading through the documentation and taking the advice that the warning gives me, but that only yields more error messages so it leads me to believe i'm "fixing" the wrong thing.