Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [ ] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame([[None,2,3], [3,4,5]], dtype=object)
df.set_index(1)

Issue Description

...\miniconda3\envs\py39\lib\site-packages\pandas\core\indexes\base.py:6982: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
  return Index(sequences[0], name=names)

Side note -- is it possible in pandas 1.4 to set_index using object types without emitting this warning? I don't see an alternative recommendation or additional argument to allow this.

Expected Behavior

The warning should show my code, not pandas code

Installed Versions

INSTALLED VERSIONS ------------------ commit : bb1f651536508cdfef8550f93ace7849b00046ee python : 3.9.7.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22000 machine : AMD64 processor : AMD64 Family 23 Model 104 Stepping 1, AuthenticAMD byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 1.4.0 numpy : 1.21.5 pytz : 2021.3 dateutil : 2.8.2 pip : 21.3.1 setuptools : 58.0.4 Cython : None pytest : 6.2.5 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.0.3 IPython : 7.30.0 pandas_datareader: None bs4 : None bottleneck : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.5.0 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : 1.7.3 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None

Comment From: phofl

Unfortunately there is no easy way around this. The stacklevel is correct if using the Index constructor, but set_index uses it under the hood. We added a function, which is able to infer the stacklevel, but it had a high performance penalty, hence we reverted it again.

cc @rhshadrach

Comment From: rhshadrach

Unfortunately there is no easy way around this.

Agreed. With the performance impact of using find_stack_level ruling it out, the only other options I see are to somehow pass the appropriate stack level as an argument or move the warning to a higher level so that it isn't emitted on every call to Index. I don't see a feasible way to accomplish either of these.

I do wonder if we should be adding a way to globally silence any warning that isn't able to be silenced in another way (Edit:) at least for a warning that might be often hit. The alternative is for users to use the catch_warnings context manager.

Comment From: rhshadrach

Thinking about this more, I don't think adding an option just to silence this warning is a good idea. Users can use catch_warnings. cc @jbrockmendel for any thoughts.

Comment From: jbrockmendel

i guess we could catch and re-issue the warning (possibly with a more apt message)

Comment From: ntjess

From a user perspective, usually cathing a future warning is a sign of code that is consigned to not be updated, i.e. rarely is it recommended to just catch a Future warning and move on. Rather, it's an indication that the accepted way of doing something has changed.

For that reason, I'd really appreciate some way if avoiding code bases needing a warnings context every time they set_index with object dtypes. Even passing an object-dtype Index into set_index generates this warning. Just my opinion though, and of course less actionable.

Comment From: mroeschke

Since we've enforced all deprecations I don't think this issue is relevant anymore, and since now we error on warnings from pandas in our test suite this should hopefully not appear as often so closing