This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 5 years ago.
I add values to a dataframe entry by entry as followed:
refined_cme_quandl_list['typical_daily_volume']= np.nan
for index, row in refined_cme_quandl_list.iterrows():
refined_cme_quandl_list['typical_daily_volume'][index] = typical_volume[row['Quandl_download_symbol']]
I still get what i want, but i get this warning:
SettingWithCopyWarning: A value is trying to be set on a copy of a
slice from a DataFrame
See the caveats in the documentation:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
Does it matter?
Yes, using boolean indexing directly to assign to slices is not recommended. Use df.loc instead:
refined_cme_quandl_list.loc[index, 'typical_daily_volume'] = \
typical_volume[row['Quandl_download_symbol']]
It is quite possible that future releases of pandas might disable this behaviour (direct indexing), so you don't want your code breaking in the future.
Related
This question already has answers here:
SettingWithCopyWarning even when using .loc[row_indexer,col_indexer] = value
(9 answers)
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed last year.
def nan_to_zeros(df, col):
new_col = f"nanreplace{col}"
df[new_col] = df[col].fillna('~')
return df
When running this function I get the
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
warning and from what I can gather I am not using a chained assignment and adding inplace=True does nothing to silence the warning.
Any direction appreciated. Thanks!
This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
SettingWithCopyWarning when using groupby and transform('first') for fillna
(1 answer)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I need to use the following code:
raw_data.loc[(raw_data['PERMNO']==10006)&(raw_data['month']>=50)&(raw_data['month']<=100)]['resi']=raw_data['RET']-raw_data['ewretd']
that is based on the conditions to calculate column 'resi'.
But I keep getting warnings like
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
"""Entry point for launching an IPython kernel.
How to correct this?
Try adding df.copy:
raw_data = raw_data.copy()
raw_data.loc[(raw_data['PERMNO']==10006)&(raw_data['month']>=50)&(raw_data['month']<=100), 'resi'] = raw_data['RET'] - raw_data['ewretd']
This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 1 year ago.
When I look at the answer to a similiar question as shown in this link: Pandas: adding column with the length of other column as value
I come across an issue where the solution its suggesting i.e
df['name_length'] = df['seller_name].str.len()
Throws the following warning
'''
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
'''
My question is: How could this be done inorder to prevent this warning from occuring? As in this command I would like to add a new column to the original dataframe not create some sort of copy of a slice.
I took a sample data set to test this issue in Python 3.8
Sample data
here is the same code which you ran
df['name_length'] = df['seller_name'].str.len()
there was no error
This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 2 years ago.
I'm trying to set some values in one column (col_2) coming from another column (col_3) when values in col_1 fulfill a condition.
I'm doing that with the .loc operator as after all my research looking for alternatives it looks as the best practice.
data.loc[data.col_1.isin(['A','B']),col_2]=data[col_3]
Nevertheless this stills trigger the 'SettingWithCopyWarning:'.
Is there any other way to do this operation without raising that warning?
Following the advice of Cameron,the line of code was part of a function, where data was passed as argument.
Starting the function with data = data.copy() solves the issue
This question already has answers here:
Pandas still getting SettingWithCopyWarning even after using .loc
(3 answers)
Closed 6 years ago.
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).
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")