0

I can't wrap my head around why this seemingly trivial piece of code pandas code below yields a SettingWithCopyWarning.

I have a DataFrame questions that contains (among others) a result column containing results to a list of closed questions: a "J" equals 1 point, an "O" equals zero points. I simply want to map J, O and other results to a zero, one, None scheme, and store the result in a new column:

def scoreMap(x):

    if x == "J":
        return 1
    elif x == "O":
        return 0
    else:
        return None

questions['closedCorrect'] = questions['result'].apply(scoreMap)

The results seem correct otherwise when I inspect them, but the warning makes me suspicious.

Can anyone indicate whether I am making a thinking error, or why the warning could/should be ignored in this case?

timbit
  • 353
  • 2
  • 11
  • In my opinion here missing `.copy()` – jezrael Jan 24 '19 at 08:19
  • I have read many such StackOverflow posts as the one you marked my question a duplicate of. I still don't understand why this happens here. Can you explain in more detail? Why should I add .copy()? Is this a "False Positive"? – timbit Jan 24 '19 at 08:33
  • 2
    My english is no so nice, but I think [coldspeed](https://stackoverflow.com/a/53954986/2901002) explain it very well. – jezrael Jan 24 '19 at 08:34
  • Thank you, this is informative. However, I still don't see why the warning is raised when I create a *new* column, like here, and thus am not changing values in the DataFrame based on any subselection. – timbit Jan 30 '19 at 13:55
  • then I think best is create [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) with your problem, not easy find problem with not good example. – jezrael Jan 30 '19 at 13:58

0 Answers0