忽略 Python pandas 中的 FutureWarnings

Ignoring FutureWarnings in Python pandas

这个问题是这里的后续问题: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

出于某种原因,这并没有忽略这些警告。我试图忽略它们来自的 pandas 中的警告,而不是我可能从其他包中获得的警告。你能告诉我为什么这不起作用吗?

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

你也可以检查这个thread。如果警告仍然存在,那就顺其自然吧,它不会阻止您的代码来自 运行.