0

This might seem like a repetitive question but despite looking at all the available solutions, I have not been able to find one for my case.

I have a dictionary of DataFrames (type as DF henceforth) where each Ticker Symbol creates a new DF. I want to iterate through each DF and update a column based on a logic. When I do that, I get the infamous "value is trying to be set on a copy of a slice..." warning and it repeats 1000's of times.

So here is the code and result. Can someone please help (even if there is a way I can suppress the warning). Thanks.

The Function is as follows. For simplicity here, instead of iterating, I am just assigning in Row 16 (15+1).

def Process_Ticker( DF_daily):
    #
    ticker = 'MMM'
    #
    print ( "DF_daily[ ticker].columns.to_list()")
    print ( DF_daily[ ticker].columns.to_list())
    #
    print ( "DF_daily[ ticker].head()")
    print ( DF_daily[ ticker].head())
    #
    i = 15
    print ( "DF_daily[ ticker].TrendScoreStr1[ i+1] = 'X'")
    DF_daily[ ticker].TrendScoreStr1[ i+1] = 'X'
    #
    return

I call this function by passing work1_daily that is a dictionary of DFs created by another function.

# work1_daily is a dictionary of DFs being created by another function
Process_Ticker( work1_daily)

This is the result I get:

DF_daily[ ticker].columns.to_list()
['Open', 'High', 'Low', 'Close', 'Volume', 'Pivot', 'R1', 'R2', 'R3', 'S1', 'S2', 'S3', 'TrendScoreStr1', 'TrendScoreStr2']

DF_daily[ ticker].head()
                       Open    High  ...  TrendScoreStr1  TrendScoreStr2
Datetime                             ...                                
2020-10-15 00:00:00     NaN     NaN  ...                                
2020-10-16 00:00:00  166.31  169.68  ...                                
2020-10-19 00:00:00  170.30  172.07  ...                                
2020-10-20 00:00:00  171.02  172.35  ...                                
2020-10-21 00:00:00  170.50  173.19  ...                                

[5 rows x 14 columns]

DF_daily[ ticker].TrendScoreStr1[ i+1] = 'X'
<ipython-input-23-2fca2e942ced>:13: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  DF_daily[ ticker].TrendScoreStr1[ i+1] = 'X'
  • There are quite several questions/answers on SO on the same topic. You should have a look at those first. In a nutshell, try: `DF_daily.loc[TrendScoreStr1[ i+1] , ticker]= 'X'` – Quang Hoang Nov 26 '20 at 15:13
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – flyingdutchman Nov 26 '20 at 16:31
  • @QuangHoang Thank you for your feedback. I had actually tried somethin similar but I ot the same error that I ot with your solution. Here is what happens: File "", line 13, in Process_Ticker DF_daily.loc[TrendScoreStr1[ i+1] , ticker]= 'X' AttributeError: 'dict' object has no attribute 'loc' – OldAgedNewbie Nov 26 '20 at 19:09
  • I am so disappointed by this ridiculous warning from Python. My same code worked fine on my Desktop where I had installed Anaconda about 10 months back. Only because I am using my laptop where I installed fresh Anaconda last week, this became a big issue. On the desktop, the warning would come only on the first instance in the loop. Now, the warning comes on EVERY instance in the loop. It makes the whole program take much longer to run. So far, none of the solutions I have found work. – OldAgedNewbie Nov 26 '20 at 20:57
  • (... contd) I even assigned the dataframe from the dataframe dictionary to a new dataframe variable but either it does not work or I get the same warning.. – OldAgedNewbie Nov 26 '20 at 20:58

0 Answers0