Which module should I specify in the call to warnings.filterwarnings()? It does not seem to be the module where the warn() was issued, certainly not with imported third-party modules.
My concrete case:
I use some third party modules that output warnings generated by pandas. I want to suppress these pandas warnings while waiting for the package maintainers to fix the code. I only want the warnings to be suppressed for the code of the third party modules; I still want to get the warnings for my own code.
It is not straightforward how to specify the module argument in the call to warnings.filterwarnings(). It seems to work differently for different third party modules!
I avoid the context manager warnings.catch_warnings() because it is not thread-safe.
Example 1: yfinance (v0.2.36)
import yfinance as yf
df = yf.Ticker("AAPL").history()
emits warning
(...)/python3.11/site-packages/yfinance/utils.py:775: FutureWarning: The 'unit' keyword in TimedeltaIndex construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead.
df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
I can suppress the warning by specifying module="yfinance":
import warnings
import yfinance as yf
warnings.filterwarnings(
"ignore",
message="The 'unit' keyword in TimedeltaIndex construction is deprecated",
category=FutureWarning,
module="yfinance",
)
df = yf.Ticker("AAPL").history()
gives no warning :-)
For info: the full call stack of the warning by simply doing warnings.filterwarnings("error"):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "(...)/python3.11/site-packages/yfinance/utils.py", line 103, in wrapper
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/yfinance/base.py", line 313, in history
quotes = utils.fix_Yahoo_dst_issue(quotes, params["interval"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/yfinance/utils.py", line 775, in fix_Yahoo_dst_issue
df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/indexes/timedeltas.py", line 170, in __new__
warnings.warn(
FutureWarning: The 'unit' keyword in TimedeltaIndex construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead.
Example 2: pandas_market_calendars
import datetime as dt
import pandas_market_calendars as mcal
at_or_before = dt.datetime.now(tz=dt.timezone.utc)
nyse = mcal.get_calendar('NYSE') # type: ignore
nyse_schedule = nyse.schedule(at_or_before - dt.timedelta(days=5), at_or_before) # type: ignore
is_open = nyse.open_at_time(nyse_schedule, at_or_before)
emits warning
<stdin>:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)
I don't find which module name I should give filterwarnings(). "pandas_market_calendars" doesn't do the trick:
import datetime as dt
import warnings
import pandas_market_calendars as mcal
warnings.filterwarnings("ignore", category=FutureWarning, module="pandas_market_calendars")
at_or_before = dt.datetime.now(tz=dt.timezone.utc)
nyse = mcal.get_calendar('NYSE')
nyse_schedule = nyse.schedule(at_or_before - dt.timedelta(days=5), at_or_before)
is_open = nyse.open_at_time(nyse_schedule, at_or_before)
still emits warning
<stdin>:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)
Even specifying "pandas" or "pandas.core.internals.blocks" (where the warning is issued) as module doesn't work.
import datetime as dt
import warnings
import pandas_market_calendars as mcal
warnings.filterwarnings("ignore", category=FutureWarning, module="pandas")
warnings.filterwarnings("ignore", category=FutureWarning, module="pandas.core")
warnings.filterwarnings("ignore", category=FutureWarning, module="pandas.core.internals")
warnings.filterwarnings("ignore", category=FutureWarning, module="pandas.core.internals.blocks")
at_or_before = dt.datetime.now(tz=dt.timezone.utc)
nyse = mcal.get_calendar('NYSE')
nyse_schedule = nyse.schedule(at_or_before - dt.timedelta(days=5), at_or_before)
is_open = nyse.open_at_time(nyse_schedule, at_or_before)
For info: the full call stack of the warning by simply doing warnings.filterwarnings("error"):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "(...)/python3.11/site-packages/pandas_market_calendars/market_calendar.py", line 815, in open_at_time
day = day.replace(self.open_close_map)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/generic.py", line 8045, in replace
return self.replace(
^^^^^^^^^^^^^
File "/home/janfr/.pyenv/versions/3.11.8/envs/jkx-3.11.8/lib/python3.11/site-packages/pandas/core/generic.py", line 8093, in replace
new_data = self._mgr.replace_list(
^^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/internals/base.py", line 278, in replace_list
bm = self.apply_with_block(
^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/internals/managers.py", line 364, in apply
applied = getattr(b, f)(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/internals/blocks.py", line 1119, in replace_list
result = blk._replace_coerce(
^^^^^^^^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/internals/blocks.py", line 1223, in _replace_coerce
return self.replace(
^^^^^^^^^^^^^
File "(...)/python3.11/site-packages/pandas/core/internals/blocks.py", line 904, in replace
warnings.warn(
FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
This puzzles me. What makes the situation for these two modules different?
More generally: what is exactly the "module" that we need to specify? The documentation is not clear on this point. It would be cool if we could specify any module on the call stack of the warning, but that does not seem the case.