【问题标题】:Ignoring FutureWarnings in Python pandas忽略 Python pandas 中的未来警告
【发布时间】:2021-06-02 01:31:08
【问题描述】:

这个问题是这里的后续问题:How to disable Python warnings? 我有以下代码:

from prettytable import PrettyTable 
import operator
import calendar
import warnings
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    warnings.filterwarnings("ignore", category=FutureWarning)
    import pandas as pd, datetime, calendar
...
{rest of my code}

我试图抑制这些警告:

<ipython-input-3-7e458c7b53b6>:22: FutureWarning: weekofyear and week have been deprecated, please use DatetimeIndex.isocalendar().week instead, which returns a Series.  To exactly reproduce the behavior of week and weekofyear and return an Index, you may call pd.Int64Index(idx.isocalendar().week)
  data['week'] = data.index.week

出于某种原因,这并没有忽略这些警告。我试图忽略它们来自的熊猫的警告,而不是我可能从其他包中得到的警告。您能否告知为什么这不起作用?

【问题讨论】:

  • 听警告并停止使用已弃用的代码不是更容易吗?
  • 我们没有去月球,因为它很容易
  • 我只想知道怎么做。我会修复代码,但为什么它不起作用?

标签: python python-3.x pandas warnings deprecation-warning


【解决方案1】:

根据我的小研究,pandas 似乎有一些棘手的方法。你试过import warnings; warnings.filterwarnings("ignore")吗?

您也可以查看thread。如果警告仍然存在,就让它继续吧,它不会阻止您的代码运行。

【讨论】:

  • 感谢您的回答,但这会忽略所有警告,而不仅仅是熊猫的警告,因为您没有提到任何包。我希望仅针对某些包而不是其他包禁止警告:)
  • 没关系,你检查线程了吗?关于这个问题确实说了很多。
猜你喜欢
  • 2016-10-03
  • 1970-01-01
  • 2010-10-27
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多