0

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.

Jason000
  • 179
  • 2
  • 5
  • 14
  • Consider reading this: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – marcos Jan 07 '20 at 18:10

2 Answers2

0

I think in the recent version 0.25, pandas has started raising it as error. Here's a work around, you can do:

df = df[['timestamp', 'cost']].copy()
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

Thanks for the replies! I changed df = df[['timestamp', 'price']] to df = df.loc[:, ('timestamp', 'price')] and it seems to work now!

Jason000
  • 179
  • 2
  • 5
  • 14