{"id":863,"hash":"07372f69ccf9d7ec5fd287f7497d0901059688a4ea4c92166030e45add82b28f","pattern":"How to deal with SettingWithCopyWarning in Pandas","full_message":"I just upgraded my Pandas from 0.11 to 0.13.0rc1. Now, the application is popping out many new warnings. One of them like this:\n\nE:\\FinReporter\\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_index,col_indexer] = value instead\n  quote_df['TVol']   = quote_df['TVol']/TVOL_SCALE\n\nWhat exactly does it mean?  Do I need to change something?\n\nHow should I suspend the warning if I insist on using quote_df['TVol']   = quote_df['TVol']/TVOL_SCALE?\n\nThe function that gives warnings\ndef _decode_stock_quote(list_of_150_stk_str):\n    \"\"\"decode the webpage and return dataframe\"\"\"\n\n    from cStringIO import StringIO\n\n    str_of_all = \"\".join(list_of_150_stk_str)\n\n    quote_df = pd.read_csv(\n        StringIO(str_of_all),\n        sep=',',\n        names=list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg')) \n        #dtype={'A': object, 'B': object, 'C': np.float64}\n    quote_df.rename(\n        columns={\n            'A':'STK', 'B':'TOpen', 'C':'TPCLOSE', 'D':'TPrice',\n            'E':'THigh', 'F':'TLow', 'I':'TVol', 'J':'TAmt',\n            'e':'TDate', 'f':'TTime'},\n        inplace=True)\n    quote_df = quote_df.ix[:,[0,3,2,1,4,5,8,9,30,31]]\n    quote_df['TClose'] = quote_df['TPrice']\n    quote_df['RT']     = 100 * (quote_df['TPrice']/quote_df['TPCLOSE'] - 1)\n    quote_df['TVol']   = quote_df['TVol']/TVOL_SCALE\n    quote_df['TAmt']   = quote_df['TAmt']/TAMT_SCALE\n    quote_df['STK_ID'] = quote_df['STK'].str.slice(13,19)\n    quote_df['STK_Name'] = quote_df['STK'].str.slice(21,30)#.decode('gb2312')\n    quote_df['TDate']  = quote_df.TDate.map(lambda x: x[0:4]+x[5:7]+x[8:10])\n    \n    return quote_df\n\nMore warning messages\nE:\\FinReporter\\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_index,col_indexer] = value instead\n  quote_df['TVol']   = quote_df['TVol']/TVOL_SCALE\nE:\\FinReporter\\FM_EXT.py:450: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_index,col_indexer] = value instead\n  quote_df['TAmt']   = quote_df['TAmt']/TAMT_SCALE\nE:\\FinReporter\\FM_EXT.py:453: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_index,col_indexer] = value instead\n  quote_df['TDate']  = quote_df.TDate.map(lambda x: x[0:4]+x[5:7]+x[8:10])","ecosystem":"pypi","package_name":"pandas","package_version":null,"solution":"The SettingWithCopyWarning was created to flag potentially confusing \"chained\" assignments, such as the following, which does not always work as expected, particularly when the first selection returns a copy.  [see GH5390 and GH5597 for background discussion.]\n\ndf[df['A'] > 2]['B'] = new_val  # new_val not set in df\n\nThe warning offers a suggestion to rewrite as follows:\n\ndf.loc[df['A'] > 2, 'B'] = new_val\n\nHowever, this doesn't fit your usage, which is equivalent to:\n\ndf = df[df['A'] > 2]\ndf['B'] = new_val\n\nWhile it's clear that you don't care about writes making it back to the original frame (since you are overwriting the reference to it), unfortunately this pattern cannot be differentiated from the first chained assignment example. Hence the (false positive) warning. The potential for false positives is addressed in the docs on indexing, if you'd like to read further.  You can safely disable this new warning with the following assignment.\n\nimport pandas as pd\npd.options.mode.chained_assignment = None  # default='warn'\n\nOther Resources\n\npandas User Guide: Indexing and selecting data\nPython Data Science Handbook: Data Indexing and Selection\nReal Python: SettingWithCopyWarning in Pandas: Views vs Copies\nDataquest: SettingwithCopyWarning: How to Fix This Warning in Pandas\nTowards Data Science: Explaining the SettingWithCopyWarning in pandas","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas","votes":1494,"created_at":"2026-04-19T04:51:53.055088+00:00","updated_at":"2026-04-19T04:51:53.055088+00:00"}