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'