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