How to ignore warnings in Python

Modified: | Tags: Python, Error handling

In Python, you can use the warnings module from the standard library to control warnings, such as ignoring (suppressing) warnings or turning matching warnings into exceptions.

Although the warnings module provides a warn() function to issue warnings, this article will not cover it. Please refer to the official documentation if you want to issue warnings in your own functions.

All sample code in this article assumes that the following modules have been imported. The pandas.DataFrame is only used as an example of a warning; its specific contents aren't our focus here.

import warnings
import pandas as pd

df = pd.DataFrame([[0, 1, 2], [3, 4, 5]])

Examples of warnings

SyntaxWarning by comparing literals with is

Comparing string or numeric literals with is will trigger a SyntaxWarning.

print(100 is 100)
# True
# 
# <>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
# <>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
# /var/folders/rf/b7l8_vgj5mdgvghn_326rn_c0000gn/T/ipykernel_60077/3973932639.py:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
#   print(100 is 100)

SettingWithCopyWarning in pandas

A SettingWithCopyWarning is issued for chained assignments in pandas.

df.iloc[:1][0] = 0
# /var/folders/rf/b7l8_vgj5mdgvghn_326rn_c0000gn/T/ipykernel_60077/1345802814.py:1: SettingWithCopyWarning: 
# 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
#   df.iloc[:1][0] = 0

Ignore warnings

Use warnings.simplefilter() to change the handling of warnings and warnings.resetwarnings() to reset.

Ignore all warnings

All warnings are ignored by setting the first argument action of warnings.simplefilter() to 'ignore'.

warnings.simplefilter('ignore')

print(100 is 100)
# True

df.iloc[:1][0] = 0

Besides 'ignore', for the action argument, you can specify 'once', which issues a warning only the first time it occurs. See the official documentation for other options available for action.

Specify warning categories to ignore

You can specify a warning category for the second argument category of warnings.simplefilter(). Warning categories include FutureWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, etc.

By default, category is set to Warning, the base class for all warning category classes. As in the example above, this means all warnings are covered.

Warning categories are described in the warning message. As mentioned above, is comparison for literals issues a SyntaxWarning and chained assignment issues a SettingWithCopyWarning.

For example, if category=SyntaxWarning, warnings for is comparison for literals are disabled, but warnings for chained assignments are still issued.

warnings.resetwarnings()

warnings.simplefilter('ignore', SyntaxWarning)

print(100 is 100)
# True

df.iloc[:1][0] = 0
# /var/folders/rf/b7l8_vgj5mdgvghn_326rn_c0000gn/T/ipykernel_60077/1345802814.py:1: SettingWithCopyWarning: 
# 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
#   df.iloc[:1][0] = 0

The warning category for chained assignments, SettingWithCopyWarning, is defined in pandas. You need to specify it as pd.core.common.SettingWithCopyWarning, not merely SettingWithCopyWarning.

warnings.resetwarnings()

# warnings.simplefilter('ignore', SettingWithCopyWarning)
# NameError: name 'SettingWithCopyWarning' is not defined

warnings.simplefilter('ignore', pd.errors.SettingWithCopyWarning)

print(100 is 100)
# True
# 
# <>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
# <>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
# /var/folders/rf/b7l8_vgj5mdgvghn_326rn_c0000gn/T/ipykernel_60077/3973932639.py:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
#   print(100 is 100)

df.iloc[:1][0] = 0

Treat warnings as exceptions

Unlike exceptions, the process continues to run, even if a warning is issued.

If you want to stop the process for warnings like exceptions, set action of warnings.simplefilter() to 'error'.

warnings.resetwarnings()

warnings.simplefilter('error')

# print(100 is 100)
# SyntaxError: "is" with a literal. Did you mean "=="?

The second argument category can be used to specify the warning category to be targeted. You can also specify separate actions for each category.

warnings.resetwarnings()

warnings.simplefilter('ignore', SyntaxWarning)
warnings.simplefilter('error', pd.errors.SettingWithCopyWarning)

print(100 is 100)
# True

# df.iloc[:1][0] = 0
# SettingWithCopyWarning: ...

Temporarily control warnings

If you want to control warnings temporarily, use with and warnings.catch_warnings().

The settings applied using warnings.simplefilter() are effective only inside the with block.

warnings.resetwarnings()

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    df.iloc[:1][0] = 0

df.iloc[:1][0] = 0
# /var/folders/rf/b7l8_vgj5mdgvghn_326rn_c0000gn/T/ipykernel_60077/1345802814.py:1: SettingWithCopyWarning: 
# 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
#   df.iloc[:1][0] = 0

In Python 3.11, the action and category arguments were added to warnings.catch_warnings(). They are treated as passed to the action and category arguments of warnings.simplefilter().

with warnings.catch_warnings(action='ignore', category=pd.errors.SettingWithCopyWarning):
    df.iloc[:1][0] = 0

Related Categories

Related Articles