1

Im desperate with this warning. I'm doing the following:

groups = df.groupby('year')
2018_group = groups.get_group('2018')

if not 2018_group['Descripcion'].empty:
    desc = 2018_group.loc[2018_group['Descripcion'].notnull(), 'Desc'].copy()
    2018_group.loc[:, 'Descripcion'] = desc.unique()[0]
    print 2018_group

Getting the known error:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  2018_group.loc['Desc'] = desc.unique()[0]

What I want to do is fill the column 'Desc' with the non-empty value in that column

Solar
  • 445
  • 1
  • 4
  • 12
  • @Idlehands I've checked that post without success doing exactly the same and also reading docs... that's the reason I'm asking now – Solar Jan 31 '19 at 16:43

1 Answers1

2

The problem is earlier in your code: 2018_group represents a slice of your dataframe.

So copy the slice before modifying it:

2018_group = groups.get_group('2018').copy()

As an aside, you make a copy in your definition of desc for no visible purpose.

jpp
  • 159,742
  • 34
  • 281
  • 339