1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

Organization

Pandasでwarningが発生した箇所の見つけ方

背景

pandasでwarningが発生したけど、どこで発生したかわからないことがたまにあります。そのようなときはpandasのオプションをセットして、warningではなくerrorをraiseするように設定すれば、warningの発生箇所を素早く特定できます。

pd.options.mode.chained_assignment = 'raise'

import pandas as pd
pd.options.mode.chained_assignment = 'raise'

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
df2 = df[df['a'] == 2]
df2[df2['b'] == 5] = 1

出力

Traceback (most recent call last):
  File "/Users/hogehoge/python/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-37a0949d6b87>", line 10, in <module>
    df2[df2['b'] == 5] = 1
  File "/Users/hogehoge/.pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/pandas/core/frame.py", line 2935, in __setitem__
    self._setitem_array(key, value)
  File "/Users/hogehoge/.pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/pandas/core/frame.py", line 2956, in _setitem_array
    self._check_setitem_copy()
  File "/Users/hogehoge/.pyenv/versions/anaconda3-2019.10/lib/python3.7/site-packages/pandas/core/generic.py", line 3729, in _check_setitem_copy
    raise com.SettingWithCopyError(t)
pandas.core.common.SettingWithCopyError: 
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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
1
Help us understand the problem. What are the problem?