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
1
vote
1 answer

Where does this pandas warning come from?

I have a DataFrame. To do a statistical conditional test, I split it into two based on a boolean column ('mar'). I want to use the ratio of counts between the two tables to add a column expressing the proportion of true values in the 'mar' column…
kingledion
  • 2,263
  • 3
  • 25
  • 39
1
vote
1 answer

How to insert a column into a DataFrame without generating the SettingWithCopyWarning

I would like to insert a column into an existing DataFrame. Ideally without copying existing data. Whatever I try, later assignments to the resulting DataFrame generate a SettingWithCopyWarning, if the inserted data contains null values. import…
Konstantin
  • 2,451
  • 1
  • 24
  • 26
1
vote
1 answer

What is the benefit of a dataframe view or copy

I've seen many questions about the infamous SettingWithCopy warning. I've even ventured to answer a few of them. Recently, I was putting together an answer that involved this topic and I wanted to present the benefit of a dataframe view. I failed…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
1
vote
1 answer

Pandas: Get SettingWithCopyWarning when using set_categories

I have two data frame. Both have the same set of columns but some columns are categorical typed (based on the actual containing values). In order to combine them I refresh the categorical type of the categorical columns with the union of both…
AnnetteC
  • 490
  • 2
  • 5
  • 20
0
votes
2 answers

Why do I get a SettingWithCopyWarning when using a MultiIndex (but not with a simple index)?

The following piece of code works as expected, with no warnings. I create a dataframe, create two sub-dataframes from it using .loc, give them the same index and then assign to a column of one of them. import numpy as np import pandas as pd df =…
schtandard
  • 387
  • 4
  • 18
0
votes
1 answer

Is there a safe and efficient way to fill NaNs in pandas only in a specific time range of the day?

I know that chained-assignment in pandas is definitely a hot topic and there are a huge amount of questions on it but I am still unable to find a solution that works in my case. I am working with irradiance and pv time series data (pandas dataframe…
Rick
  • 11
  • 2
0
votes
1 answer

python - chained assignment - receive SettingWithCopyWarning but still changing the original df?

I have a dataframe: jb = pd.DataFrame([ ['No', 75, 2.0], ['Blofeld', 140, 1.9], ['Chiffre', 114, 1.7] ], index=['b1', 'b5', 'b21'], columns=['Name', 'Weight', 'Height']) Then if I do chained assignment as below, it won't change…
wwj123
  • 365
  • 2
  • 12
0
votes
1 answer

Why does the order matter in this chained assignment?

As shown by the following two code snippets, the order of the chained assignment matters (i.e. node = node[ch] = {} is not equivalent to node[ch] = node = {}. trie = {} node = trie for ch in "word": if ch in node: node = node[ch] …
joseville
  • 685
  • 3
  • 16
0
votes
0 answers

What is the alternative for python panda chained assignment

I'm trying to add new columns into a new excel with addition to existing column using pandas chained assignment. But this is so slow and memory consumption to high and showing below warning See the caveats in the documentation:…
Aaditya R Krishnan
  • 495
  • 1
  • 10
  • 31
0
votes
0 answers

Unexpected SettingWithCopyWarning:

I'm running code that modifies values within a certain threshold in a dataframe. I receive a warning that on the surface does not seem warranted: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using…
gciriani
  • 611
  • 2
  • 7
  • 19
0
votes
1 answer

I got around a SettingWithCopyWarning, feels like the wrong way and computationally inefficient, is there a better way?

I encountered the ever-common SettingWithCopyWarning when trying to change some values in a DataFrame. I found a way to get around this without having to disable the warning, but I feel like I've done it the wrong way, and that it is needlessly…
0
votes
1 answer

pandas set partial data and get SettingWithCopyWarning

I tried to set the partial data to -1, but I get a SettingWithCopyWarning. I tried to find StackOverflow, but lots of answers use loc to solved. The data comes from Kaggle Titanic. import pandas as pd train = pd.read_csv('data/train.csv') y =…
sappy
  • 770
  • 2
  • 6
  • 16
0
votes
1 answer

Setting values on pandas DataFrame with multiindex

The following is a minimal example of what I am trying to do. I have a pandas DataFrame with multiindex as follows import pandas as pd import numpy as np arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two',…
user3821012
  • 1,291
  • 2
  • 16
  • 27
0
votes
1 answer

How much chained assignment possible in java?

For example int a,b,c,d,e,f,g,h,.........................; a=b=c=d=e=f=g=h=.............................=1; so, how long it can be possible in java....
ajay rathod
  • 3
  • 1
  • 3
0
votes
1 answer

Can't get around Pandas Series SettingWithCopyWarning

I'd like to fetch a Series and make changes to it, which I'd like reflected in the DataFrame later on. However I can't understand how to do it without the SettingWithCopyWarning. Is this a false positive or am I doing something wrong? df =…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147