LoginSignup
1
1

More than 3 years have passed since last update.

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

Posted at

背景

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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1