Questions tagged [chained-assignment]

Chaining of consecutive and separate index operations with Python-pandas objects.

Chained assignment (or chained indexing) as it relates to indexing is a chaining of consecutive and separate index operations, where successive index operations are done on a copy of a portion of the result from the first operation.

Assignment with chained indexing will generate a SettingWithCopyWarning and should be generally be avoided. However, there are a minority of instances where the warning can be safely ignored. For these situations consider using:

with pd.option_context('mode.chained_assignment', None):
    # Code inside this `with` block will not issue the warning

For more see:

46 questions
23
votes
3 answers

Pandas still getting SettingWithCopyWarning even after using .loc

At first, I tried writing some code that looked like this: import numpy as np import pandas as pd np.random.seed(2016) train = pd.DataFrame(np.random.choice([np.nan, 1, 2], size=(10, 3)), columns=['Age', 'SibSp',…
Huey
  • 2,714
  • 6
  • 28
  • 34
20
votes
4 answers

Pandas: SettingWithCopyWarning

I'd like to replace values in a Pandas DataFrame larger than an arbitrary number (100 in this case) with NaN (as values this large are indicative of a failed experiment). Previously I've used this to replace unwanted values: sve2_all[sve2_all['…
Jason
  • 4,346
  • 10
  • 49
  • 75
18
votes
1 answer

Pandas: Chained assignments

I have been reading this link on "Returning a view versus a copy". I do not really get how the chained assignment concept in Pandas works and how the usage of .ix(), .iloc(), or .loc() affects it. I get the SettingWithCopyWarning warnings for the…
Zhubarb
  • 11,432
  • 18
  • 75
  • 114
15
votes
1 answer

Unpredictable pandas slice assignment behavior with no SettingWithCopyWarning

It's well known (and understandable) that pandas behavior is essentially unpredictable when assigning to a slice. But I'm used to being warned about it by SettingWithCopy warning. Why is the warning not generated in either of the following two code…
max
  • 49,282
  • 56
  • 208
  • 355
10
votes
1 answer

df.loc causes a SettingWithCopyWarning warning message

The following line of my code causes a warning : import pandas as pd s = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) s.loc[-1] = [5,np.nan,np.nan,6] grouped = s.groupby(['A']) for key_m, group_m in grouped: …
Anthony Lethuillier
  • 1,489
  • 4
  • 15
  • 33
5
votes
1 answer

Action with pandas SettingWithCopyWarning

I try to delete some column and convert some value in column with df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True) df2['date'] = df2['date'].map(lambda x: str(x)[1:]) df2['date'] = df2['date'].str.replace(':', ' ', 1) df2['date'] =…
NineWasps
  • 2,081
  • 8
  • 28
  • 45
4
votes
2 answers

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

My pandas dataframe: dframe = pd.DataFrame({"A":list("abcde"), "B":list("aabbc"), "C":[1,2,3,4,5]}, index=[10,11,12,13,14]) A B C 10 a a 1 11 b a 2 12 c b 3 13 d b 4 14 e c 5 My desired output: A B C a …
ramesh
  • 1,187
  • 7
  • 19
  • 42
4
votes
1 answer

Identify view or copy of another pandas DataFrame

I wonder how I could identify whether I have a view or a copy of another dataframe. Given a pandas.DataFrame import pandas as pd df = pd.DataFrame( {'a': [0,8,15], 'b': [42,11,0] } ) as well as a view df1 = df.loc[ 1:2 ] and a copy df2 = df.loc[…
desiato
  • 1,122
  • 1
  • 9
  • 16
3
votes
1 answer

Creating new dataframe from existing - SettingWithCopyWarning

I have a csv file that I import as a dataframe. This dataframe goes through multiple filtering steps. Data is also moved between columns based on conditionals. import numpy as np import pandas as pd df = pd.read_csv('my_csv_file.csv',…
Albert Han
  • 109
  • 1
  • 2
  • 9
2
votes
0 answers

Avoiding 'SettingwithCopyWarning' while converting column data with methods

I'm working with some data where I'm trying to convert an entire column to a different format (ie from object to datetime or from object to numeric) using methods not resetting values. Each line of code below returns the 'SettingwithCopyWarning'…
exlo
  • 315
  • 1
  • 8
  • 20
2
votes
3 answers

python performance problems using loops with big tables

I am using python and multiple libaries like pandas and scipy to prepare data so I can start deeper analysis. For the preparation purpose I am for instance creating new columns with the difference of two dates. My code is providing the expected…
Bene
  • 209
  • 1
  • 9
2
votes
2 answers

SettingwithCopy when creating new column and when dropping NaN rows

I've been searching around reading the pandas docs here and trying different lines of code from questions posted around here and here and I can't seem to get away from the setting with copy warning. I'd prefer to learn to code it the "right" way as…
Monty
  • 781
  • 2
  • 6
  • 23
2
votes
2 answers

Pandas: SettingWithCopyWarning, trying to understand how to write the code better, not just whether to ignore the warning

I am trying to change all date values in a spreadsheet's Date column where the year is earlier than 1900, to today's date, so I have a slice. EDIT: previous lines of code: df=pd.read_excel(filename)#,usecols=['NAME','DATE','EMAIL'] #regex to remove…
mattrweaver
  • 729
  • 4
  • 14
  • 36
1
vote
1 answer

Chained assignment for mutable types

Ran into this issue when debugging a piece of code. If was not aware of this behaviour previously. foo = bar = [1, 2, 3] hex(id(foo)) Out[121]: '0x1f315dafe48' hex(id(bar)) Out[122]: '0x1f315dafe48' Both "variables" are pointing to the same memory…
NotAName
  • 3,821
  • 2
  • 29
  • 44
1
vote
1 answer

Receiving SettingWithCopyWarning. Is it safe to proceed?

I am trying to replace the column 'let' in the DataFrame london(which is a copy of another no_eco) with rows that only contain the strings in the contains() method. The code is as follows: london = no_eco london.loc[:,'let'] =…
geds133
  • 1,503
  • 5
  • 20
  • 52