How to suppress Pandas Future warning ?

前端 未结 3 610
猫巷女王i
猫巷女王i 2020-11-30 19:24

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

D:\\Python\\lib\\site-packages\\pandas\\core\\frame.py:3581: FutureWarning: re         


        
相关标签:
3条回答
  • 2020-11-30 20:15

    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)
    
    0 讨论(0)
  • 2020-11-30 20:22

    @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!

    0 讨论(0)
  • 2020-11-30 20:24

    Found this on github...

    import warnings
    warnings.simplefilter(action='ignore', category=FutureWarning)
    
    import pandas
    
    0 讨论(0)
提交回复
热议问题