SettingWithCopyWarning despite df.loc

Loris Bennett loris.bennett at fu-berlin.de
Wed Jun 16 07:41:35 EDT 2021


Hi,

I have read a CSV file into a pandas DataFrame.  The data contain a
column for disk-space usage in KB.  To plot the data, I would like to
scale the date to, say, GB.  What I have is the following:

  size_unit = "GB"
  factor = {"GB": 1/(1024*1024)}

  usage.loc[:, size_unit] = usage.loc[:, 'KB']
  usage.loc[:, size_unit] = usage.loc[:, size_unit] * factor[size_unit]

This works, but I get the warning:

  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
    self.obj[item] = s

I added the .loc stuff after getting the same warning from my naive
attempts at copying and modifying an existing column and then trying to
understand the information pointed to by the URL.  However, I don't see
what my example has to do with "chained indexing" and why what I now
have is still wrong.

Can anyone illuminate me?

Cheers,

Loris
 
-- 
This signature is currently under construction.


More information about the Python-list mailing list