Home > Enterprise >  Changing some values in a row of pd.DataFrame leads to SettingWithCopyWarning pandas python
Changing some values in a row of pd.DataFrame leads to SettingWithCopyWarning pandas python

Time:05-26

I'm making stock app, and when get data and edit it, see this error.
Code is simple:

df = yf.download(ticker, period=period, interval='1wk', auto_adjust=True, threads=True)

Here i get DataFrame like bellow:

                  Open        High         Low       Close     Volume
Date                                                                 
2020-05-25  205.940002  207.880005  196.699997  207.389999  114231300
2020-06-01  205.899994  220.589996  203.940002  219.550003   85600600
2020-06-08  219.600006  225.000000  213.559998  217.639999   68520500
2020-06-15  214.110001  226.500000  212.750000  220.639999   77023000
2020-06-22  220.919998  231.029999  213.500000  215.710007   78020200
...                ...         ...         ...         ...        ...
2022-05-02   96.410004  102.690002   88.709999   90.050003   95662500
2022-05-09   86.955002   88.639999   78.010002   87.989998  115590500
2022-05-16   87.699997   94.480003   84.730003   86.790001  107750100
2022-05-23   87.059998   87.415001   81.540001   82.470001   29212600
2022-05-25   83.720001   84.070000   81.070000   82.309998   22781455

Then i need to edit row with date "2022-05-23"

df2 = df.loc[d_str] #d_str is 2022-05-25
df.loc[dtd2_s]['Close'] = df2['Close'] #dtd2_s is 2022-05-23


df.loc[dtd2_s]['High'] = max(df2['High'], df.loc[dtd2_s]['High'])
df.loc[dtd2_s]['Low'] = min(df2['Low'], df.loc[dtd2_s]['High'])

But here i get SettingWithCopyWarning.

/Users/alex26/miniforge3/envs/rq/lib/python3.8/site-packages/pandas/core/series.py:1056: 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
  cacher_needs_updating = self._check_is_chained_assignment_possible()

Do you know how to fix it?
Thank you

CodePudding user response:

You may want to try this usage of loc[]:

df2 = df.loc[d_str] #d_str is 2022-05-25
df.loc[dtd2_s, 'Close'] = df2['Close'] #dtd2_s is 2022-05-23

df.loc[dtd2_s, 'High'] = max(df2['High'], df.loc[dtd2_s]['High'])
df.loc[dtd2_s, 'Low'] = min(df2['Low'], df.loc[dtd2_s]['High'])

Specifically, I have used loc[index_value, column_label] with one set of braces and a comma, rather than loc[index_value][column_label] which is a single call to loc[] followed by a chained call to [], which can give rise to the warning.

Here is the documentation showing the above loc[] usage.

  • Related