-1

I am getting the famous SettingWithCopyWarning when writing:

df['test'] = pd.factorize(df.test)[0]

I have tried

df.loc[:, 'test'] = pd.factorize(df.test)[0]
df.loc[:, ('test')] = pd.factorize(df.test)[0]
df[('test')] = pd.factorize(df.test)[0]
df.loc[df.index.tolist(), 'test'] = pd.factorize(df.test)[0]

but now sucess, the warning stays. I am not sure what I am doing wrong or how to deal with that. The point - and the difference to other questions at SO - that I do not want to use an indexer.

I also consulted http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy without success.

Make42
  • 12,236
  • 24
  • 79
  • 155
  • https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas Might help. – cs95 Jul 13 '17 at 12:20
  • What are you trying to do with `factorize`? – Andrew L Jul 13 '17 at 12:21
  • 1
    @AndrewL: This is just an example. The question is about the warning, not factorize. (I would have to explain the algorithm to explain, why I need this.) – Make42 Jul 13 '17 at 12:23
  • So.. ignoring the above example you're essentially trying to set an entire column to a single value? This should not throw the error. – Andrew L Jul 13 '17 at 12:25
  • @AndrewL: No, I don't. factorize is not doing that. Please check out https://pandas.pydata.org/pandas-docs/stable/generated/pandas.factorize.html - it is returning a tuple. – Make42 Jul 13 '17 at 12:28
  • @cᴏʟᴅsᴘᴇᴇᴅ: There is no solution there I haven't tried. – Make42 Jul 13 '17 at 12:29
  • Again, a small amount of context around what you're trying to set the column to might help us help you. You are setting `test` to an array? – Andrew L Jul 13 '17 at 12:29
  • @AndrewL: There is a column called `cycleNr` which has lots of integers which correspond to signal segments that I compare with each other. The result of each comparison. will be placed into a numpy array, where rows correspond to segment1 and columns to segment2 In order to find the right cell to place my results in, I need the segments to have numbers that are from 0 to N, where N is the number of segments. `cycleNr` is a uuid for the segment, but is not in 0..N. The factorization is producing a new uuid if you will. Clear now? – Make42 Jul 13 '17 at 12:35
  • df is a slice of another dataframe. Set `df.is_copy = None`. See Jeff's answer: https://stackoverflow.com/a/20644369/2285236 – ayhan Jul 13 '17 at 13:39

1 Answers1

0

If your only issue is to remove the warning, maybe try :

pd.options.mode.chained_assignment = None 

That doesn't change anything beside not showing the warning

AZJB
  • 76
  • 8