Skip to content

multi-dimensional-indexing deprecation warning has inconsistent stack level. #31479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tacaswell opened this issue Jan 31, 2020 · 3 comments · Fixed by #41969
Closed

multi-dimensional-indexing deprecation warning has inconsistent stack level. #31479

tacaswell opened this issue Jan 31, 2020 · 3 comments · Fixed by #41969
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas
Milestone

Comments

@tacaswell
Copy link
Contributor

Code Sample, a copy-pastable example if possible

# Your code here
import warnings
import pandas as pd

warnings.simplefilter("always")


x = pd.Series([], dtype="float64")

df = pd.DataFrame(
    {
        "year": [2018, 2018, 2018],
        "month": [1, 1, 1],
        "day": [1, 2, 3],
        "value": [1, 2, 3],
    }
)
df["date"] = pd.to_datetime(df[["year", "month", "day"]])
monthly = df[["date", "value"]].groupby(["date"]).sum()
dates = monthly.index

dates[:, None]
x.index[:, None]

Problem description

Running the above script gives:

sharon@22:14  ➤  python /tmp/pd_test.py 
/tmp/pd_test.py:21: DeprecationWarning: Support for multi-dimensional indexing (e.g. `index[:, None]`) on an Index is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.
  dates[:, None]
/home/tcaswell/.virtualenvs/sys37/lib/python3.7/site-packages/pandas/core/indexes/range.py:708: DeprecationWarning: Support for multi-dimensional indexing (e.g. `index[:, None]`) on an Index is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.
  return super().__getitem__(key)


I think the issue is that

if np.ndim(result) > 1:
warnings.warn(
"Support for multi-dimensional indexing (e.g. `index[:, None]`) "
"on an Index is deprecated and will be removed in a future "
"version. Convert to a numpy array before indexing instead.",
DeprecationWarning,
stacklevel=3,
)
is pushing the warning up a fixed number of stack layers, but due to implementation details it maybe deeper / less deep than expected.

Expected Output

pandas should be the module reported in the warning about slicing in both cases or neither case. You may want to use something like https://github.com/matplotlib/matplotlib/blob/e4dbf4e7b5241e7d74f79adc854f84a954b45e8f/lib/matplotlib/cbook/__init__.py#L2013-L2032 to find the first stack not in pandas to put the warning at.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.0.0-38-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.0rc0
numpy : 1.16.5
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.2
setuptools : 41.0.1
Cython : 0.29.13
pytest : 3.10.1
hypothesis : None
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.12.0.dev
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.0rc2.post1219+g3d8a6cb83
numexpr : 2.7.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 3.10.1
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : 0.14.0
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None

@tacaswell tacaswell changed the title multi-index deprecation warning has inconsistent stack level. multi-dimensional-indexing deprecation warning has inconsistent stack level. Jan 31, 2020
@jorisvandenbossche
Copy link
Member

Yes, the problem here is that the place where the warning is raised can be reached in different ways (and so potentially needing a different stacklevel).
(more specifically, I think for all types the stacklevel is fine, except for RangeIndex, because its __getitem__ calls super().__getiitem__(key) leading to an extra stack)

Such a "dynamic stack-level inferring" sounds interesting. Your experience with it in matplotlib is good? It has proven robust?

@jorisvandenbossche jorisvandenbossche added the Warnings Warnings that appear or should be added to pandas label Jan 31, 2020
@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Aug 5, 2020
@jbrockmendel
Copy link
Member

We now have pandas.util._exceptions.find_stack_level doing pretty much exactly what matplotlib's _warn_external does. I've been trying to use the "cheat" sparingly, but if its needed, this is now an easy fix.

@tacaswell
Copy link
Contributor Author

Your experience with it in matplotlib is good? It has proven robust?

answering 17month later, we have not had any reported issues with it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants