0

I'm trying to modify a single "cell" in a dataframe. Now, modification works, but I get this warning:

In [131]: df.loc[df['Access date'] == '06/01/2016 00:35:34', 'Title'] = 'XXXXXXXX'
ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Per Pandas: Replacing column values in dataframe I am using .loc method, yet I get this warning (I don't see a copy of dataframe that I'm supposedly modifying anywhere here)

Should this warning happen here? If not, how do I disable it?

UPDATE

It seems that df is a (weakref) copy of another dataframe (checked with .is_copy).

Community
  • 1
  • 1
LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
  • I'm not terribly familiar with pandas yet, wouldn't you want `df.loc[df['Access date'] == '06/01/2016 00:35:34'].loc['Title'] ='XXXXXX'` to prevent the warning? (I have no clue if that does the thing that you want, of course) – Wayne Werner Aug 11 '16 at 15:28
  • The line you posted has no problems. `df` is probably a slice of another DataFrame. Can you check `df.is_copy`? – ayhan Aug 11 '16 at 15:30
  • its just an FYI warning, the code still will run. – Merlin Aug 11 '16 at 15:31
  • I believe the OP knows that the code will run, his question is why the warning about copying even though he's using the recommended way of assignment to avoid copying. @LetMeSOThat4U can you give an example of your data frame so we can test it ourselves? – sirfz Aug 11 '16 at 15:35
  • @sirfz Unfortunately I'm behind firewall that prevents me from uploading a file with dataframe to googledrive or such. Unless there's some pure-text style serialization method that I could use to paste an example into my question? – LetMeSOThat4U Aug 11 '16 at 15:38
  • @LetMeSOThat4U just paste a sample with a few rows. Something like `df = pd.DataFrame({'Access date': ['06/01/2016 00:35:34', ...], 'column_b': [1, ...]}, index=[...])` – sirfz Aug 11 '16 at 15:39
  • @ayhan I get this: In [142]: b.is_copy Out[142]: – LetMeSOThat4U Aug 11 '16 at 15:40
  • Perhaps `df['Access date'] == ...` is creating a copy, try `df.loc[:, 'Access date'] == ...` instead – sirfz Aug 11 '16 at 15:41
  • 1
    @LetMeSOThat4U That's the problem. You created that DataFrame by taking a slice from another DataFrame. This warning is to warn you that the changes you do in `b` will not be reflected in the actual DataFrame you used to construct b. If you don't intend to change the actual DataFrame, set `b.is_copy = None` – ayhan Aug 11 '16 at 15:41
  • @ayhan put that in the answer pls – LetMeSOThat4U Aug 11 '16 at 15:50
  • Actually now that we have confirmed that it is a slice, it would be better to close this as a duplicate so that future readers will have a more detailed and clear answer. – ayhan Aug 11 '16 at 15:53

1 Answers1

0

That link in the warning addresses the issue in detail under the section: Why does assignment fail when using chained indexing?

Summary of the section: pandas makes no guarantee on the memory handling of arrays in certain situations so the warning is there, even with certain implementations of .loc, to tell you that this could be wildly inefficient.

To turn off warnings, you can use the warnings library and execute the following code in one of your ipython notebook cells.

import warnings
warnings.catch_warnings()
warnings.simplefilter("ignore")
breucopter
  • 321
  • 1
  • 6