How to suppress Pandas Future warning?
Asked Answered
D

7

283

When I run the program, Pandas gives 'Future warning' like below every time.

D:\Python\lib\site-packages\pandas\core\frame.py:3581: 
FutureWarning: rename with inplace=True  will return None from pandas 0.11 onward
  " from pandas 0.11 onward", FutureWarning) 

I got the message, but I just want to stop Pandas showing such message again and again. Is there any builtin parameter that I can set to make Pandas stop popping up the 'Future warning'?

Donkey answered 3/4, 2013 at 2:37 Comment(0)
P
572

Found this on github...

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

import pandas
Parsifal answered 3/4, 2013 at 3:19 Comment(3)
nb: put the warnings....ignore before the import pandas... to cause the FutureWarning to be ignored.Kiri
Hey I'm getting the future warning despite adding these lines.Adolpho
It's working. Vote up.Pupa
M
90

Here is the context manager version, if you only want to suppress warnings for specific lines of code.

import warnings
with warnings.catch_warnings():
    warnings.simplefilter(action='ignore', category=FutureWarning)
    # Warning-causing lines of code here
Moralist answered 6/3, 2021 at 10:53 Comment(3)
I feel like this should be the accepted answer. It feels wrong to disable warnings at a module level. If you know of a warning to disable, you should also know the source and disable that specific piece of code. Otherwise, you don't know what other warnings you may be disabling.Lunneta
I don't think you always want to "disable specific pieces of code" necessarily. I get the following quite often (setting the values in place has always been my intention). DeprecationWarning: In a future version, df.iloc[:, i] = newvals will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either df[df.columns[i]] = newvals or, if columns are non-unique, df.isetitem(i, newvals)Isologous
It would be a lot less annoying if pandas only printed these warnings once. When you try and run a legacy script and it prints hundreds of the same warning it's just noise. You still want to know that the script contains legacy code though.Dodds
S
40

@bdiamante's answer may only partially help you. If you still get a message after you've suppressed warnings, it's because the pandas library itself is printing the message. There's not much you can do about it unless you edit the Pandas source code yourself. Maybe there's an option internally to suppress them, or a way to override things, but I couldn't find one.


For those who need to know why...

Suppose that you want to ensure a clean working environment. At the top of your script, you put pd.reset_option('all'). With Pandas 0.23.4, you get the following:

>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)

C:\projects\stackoverflow\venv\lib\site-packages\pandas\core\config.py:619: FutureWarning: html.bord
er has been deprecated, use display.html.border instead
(currently both are identical)

  warnings.warn(d.msg, FutureWarning)

: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

C:\projects\stackoverflow\venv\lib\site-packages\pandas\core\config.py:619: FutureWarning:
: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

  warnings.warn(d.msg, FutureWarning)

>>>

Following the @bdiamante's advice, you use the warnings library. Now, true to it's word, the warnings have been removed. However, several pesky messages remain:

>>> import warnings
>>> warnings.simplefilter(action='ignore', category=FutureWarning)
>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)


: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

>>>

In fact, disabling all warnings produces the same output:

>>> import warnings
>>> warnings.simplefilter(action='ignore', category=Warning)
>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)


: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

>>>

In the standard library sense, these aren't true warnings. Pandas implements its own warnings system. Running grep -rn on the warning messages shows that the pandas warning system is implemented in core/config_init.py:

$ grep -rn "html.border has been deprecated"
core/config_init.py:207:html.border has been deprecated, use display.html.border instead

Further chasing shows that I don't have time for this. And you probably don't either. Hopefully this saves you from falling down the rabbit hole or perhaps inspires someone to figure out how to truly suppress these messages!

Swaine answered 21/12, 2018 at 19:29 Comment(1)
I've found this to work with pandas import warnings; warnings.filterwarnings("ignore")Incantatory
H
21

Warnings are annoying. As mentioned in other answers, you can suppress them using:

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

But if you want to handle them one by one and you are managing a bigger codebase, it will be difficult to find the line of code which is causing the warning. Since warnings unlike errors don't come with code traceback. In order to trace warnings like errors, you can write this at the top of the code:

import warnings
warnings.filterwarnings("error")

But if the codebase is bigger and it is importing bunch of other libraries/packages, then all sort of warnings will start to be raised as errors. In order to raise only certain type of warnings (in your case, its FutureWarning) as error, you can write:

import warnings
warnings.simplefilter(action='error', category=FutureWarning)
Harney answered 27/9, 2019 at 15:45 Comment(0)
B
21

Just put this line before starting the code.

import warnings
warnings.filterwarnings("ignore")
Barb answered 10/10, 2021 at 14:31 Comment(0)
E
4

You may want to suppress some FutureWarnings but want to see others. For example, you may want to suppress FutureWarning related to plotting time-series data (because it's a temporary issue that will be resolved by pandas devs in later updates) but want to see any other FutureWarnings.

Pass a specific message

You can pass a regex to filterwarnings() to suppress FutureWarnings with specific messages. In the example below, we globally suppressed the FutureWarning whose message contains the string: "Use a DatetimeIndex" (this is the one related to plotting time-series data). Any other FutureWarnings will be shown.

import warnings
import pandas as pd
warnings.filterwarnings(action='ignore', category=FutureWarning, 
                        message=r".*Use a DatetimeIndex.*")

data = pd.Series(range(10), pd.date_range('2023', periods=10, freq='B'))
ax = data.plot()         # <--- suppressed FutureWarning
data[-1] = 10            # <--- FutureWarning

Use context manager

Another way to suppress a warning could be to run the offending code in a context manager (like in Mikko Haavisto's answer on this page). However, unlike their answer, you can pass the category of the warning and the action to take directly into the catch_warnings context.

with warnings.catch_warnings(action='ignore', category=FutureWarning):
    ax = data.plot()     # <--- silenced FutureWarning

data[-1] = 10            # <--- FutureWarning outside of context 
Esteban answered 13/12, 2023 at 1:47 Comment(1)
I was wondering why this isn't the most upvoted answer and realized it was posted yesterday on an old question. This is a great solution, it only blocks 1 specific error message. In a line of code.Linnea
C
0

I think you can use this: there's a module in Python, called warnings:

import warnings
import pandas as pd

warnings.simplefilter(action='ignore', category=FutureWarning)

Centering answered 7/4 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.