Skip to content
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

pyplot.plot using pandas series raises DeprecationWarning with pandas=1.0.0rc0 #16295

Closed
MaozGelbart opened this issue Jan 22, 2020 · 6 comments · Fixed by #16347 or #16379
Closed

pyplot.plot using pandas series raises DeprecationWarning with pandas=1.0.0rc0 #16295

MaozGelbart opened this issue Jan 22, 2020 · 6 comments · Fixed by #16347 or #16379
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Milestone

Comments

@MaozGelbart
Copy link
Contributor

Bug report

Bug summary

The code below raises DeprecationWarning from pandas=1.0.0rc0 when plt.plot is fed with two pandas.Series (using my matplotlib=3.1.0 instance). It reproduces also when the series is not empty, and also when plotting two different series as x and y.

Code for reproduction

import pandas as pd
import matplotlib.pyplot as plt

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

Actual outcome

Traceback (most recent call last):
  File "pandas1.py", line 5, in <module>
    plt.plot(x, x)
  File "/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2789, in plot
    is not None else {}), **kwargs)
  File "/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 1666, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 225, in __call__
    yield from self._plot_args(this, kwargs)
  File "/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 386, in _plot_args
    x = _check_1d(tup[0])
  File "/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 1402, in _check_1d
    x[:, None]
  File "/lib/python3.7/site-packages/pandas/core/series.py", line 865, in __getitem__
    return self._get_with(key)
  File "/lib/python3.7/site-packages/pandas/core/series.py", line 878, in _get_with
    return self._get_values_tuple(key)
  File "/lib/python3.7/site-packages/pandas/core/series.py", line 920, in _get_values_tuple
    return self._get_values(key)
  File "/lib/python3.7/site-packages/pandas/core/series.py", line 934, in _get_values
    self._data.get_slice(indexer), fastpath=True
  File "/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 1542, in get_slice
    return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True)
  File "/lib/python3.7/site-packages/pandas/core/indexes/range.py", line 708, in __getitem__
    return super().__getitem__(key)
  File "/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3908, in __getitem__
    deprecate_ndim_indexing(result)
  File "/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 5553, in deprecate_ndim_indexing
    stacklevel=3,
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.

Expected outcome

No warnings.

Matplotlib version

  • Operating system: linux
  • Matplotlib version: 3.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 3.7.3
  • Other libraries: pandas=1.0.0rc0

python and matplotlib obtained through anaconda, pandas through pip

@timhoffm timhoffm added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 25, 2020
@timhoffm timhoffm added this to the v3.1.3 milestone Jan 25, 2020
@tacaswell tacaswell modified the milestones: v3.1.3, v2.2.5 Jan 27, 2020
@tacaswell
Copy link
Member

This is something we have sort of dealt with before (#14992 / #15007 and pandas-dev/pandas#27775).

Looks like we are ready for the far future after the deprecation is done, but not the near future when it is warning, working on a PR.

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jan 27, 2020
If we catch the warning or the exception, we need to cast to numpy
because later on in `_plot_args` we again use multi-dimensional
indexing to up-cast to a 2D array.

closes matplotlib#16295
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jan 27, 2020
If we catch the warning or the exception, we need to cast to numpy
because later on in `_plot_args` we again use multi-dimensional
indexing to up-cast to a 2D array.

closes matplotlib#16295
@jklymak jklymak reopened this Jan 30, 2020
@jklymak
Copy link
Member

jklymak commented Jan 30, 2020

Re-opening because the changes in #16347 don't seem to capture the warning?

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jan 31, 2020
Not all of the warnings from pandas report that they are from pandas.

Closes matplotlib#16295 (again)
@tacaswell
Copy link
Member

Well that is deeply odd as the other test that is exercising this code path does pass.

Not sure how I missed this when I did the PR (as it fails on that test but not the other ones on rc0), sorry about that 🐑 .

Something is going wrong with the warning matching, the module filtering is picking up the warning from RangeIndex, but not from the datetime index.

It looks like the stack level stuff is ending up in a c-extension (?) and losing the module information, will report to pandas and have a PR to fix this (again).


ipython
In [1]: import pandas as pd 
   ...: import matplotlib.pyplot as plt 
   ...: import warnings 
   ...: warnings.simplefilter('error') 
   ...: x = pd.Series([], dtype="float64") 
   ...:                                                                                                                                                                                                                                                                                              

In [2]: qt5ct: using qt5ct plugin
In [2]:                                                                                                                                                                                                                                                                                              

In [2]: x.index[:, None]                                                                                                                                                                                                                                                                             
---------------------------------------------------------------------------
DeprecationWarning                        Traceback (most recent call last)
<ipython-input-2-dc66658d63da> in <module>
----> 1 x.index[:, None]

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/range.py in __getitem__(self, key)
    706             )
    707         # fall back to Int64Index
--> 708         return super().__getitem__(key)
    709 
    710     @unpack_zerodim_and_defer("__floordiv__")

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in __getitem__(self, key)
   3906         if not is_scalar(result):
   3907             if np.ndim(result) > 1:
-> 3908                 deprecate_ndim_indexing(result)
   3909                 return result
   3910             return promote(result)

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in deprecate_ndim_indexing(result)
   5546         #  cannot do that and keep an index, so return ndarray
   5547         # Deprecation GH#30588
-> 5548         warnings.warn(
   5549             "Support for multi-dimensional indexing (e.g. `index[:, None]`) "
   5550             "on an Index is deprecated and will be removed in a future "

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.

In [3]:     # Smoke test for pandas 
   ...:     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 
   ...:     forecast = monthly['value'] 
   ...:     baseline = monthly['value'] 
   ...:  
   ...:     fig, ax = plt.subplots() 
   ...:     ax.bar(dates, forecast, width=10, align='center') 
   ...:     ax.plot(dates, baseline, color='orange', lw=4)                                                                                                                                                                                                                                           
---------------------------------------------------------------------------
DeprecationWarning                        Traceback (most recent call last)
<ipython-input-3-cd3113b55513> in <module>
     14 fig, ax = plt.subplots()
     15 ax.bar(dates, forecast, width=10, align='center')
---> 16 ax.plot(dates, baseline, color='orange', lw=4)

~/source/p/matplotlib/lib/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1675         """
   1676         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1677         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1678         for line in lines:
   1679             self.add_line(line)

~/source/p/matplotlib/lib/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    213                 this += args[0],
    214                 args = args[1:]
--> 215             yield from self._plot_args(this, kwargs)
    216 
    217     def get_next_color(self):

~/source/p/matplotlib/lib/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    328 
    329         if len(tup) == 2:
--> 330             x = _check_1d(tup[0])
    331             y = _check_1d(tup[-1])
    332         else:

~/source/p/matplotlib/lib/matplotlib/cbook/__init__.py in _check_1d(x)
   1387                                         module='pandas[.*]')
   1388 
-> 1389                 ndim = x[:, None].ndim
   1390                 # we have definitely hit a pandas index or series object
   1391                 # cast to a numpy array.

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/extension.py in __getitem__(self, key)
    179 
    180         # Includes cases where we get a 2D ndarray back for MPL compat
--> 181         deprecate_ndim_indexing(result)
    182         return result
    183 

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in deprecate_ndim_indexing(result)
   5546         #  cannot do that and keep an index, so return ndarray
   5547         # Deprecation GH#30588
-> 5548         warnings.warn(
   5549             "Support for multi-dimensional indexing (e.g. `index[:, None]`) "
   5550             "on an Index is deprecated and will be removed in a future "

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.

In [4]: dates[:, None]                                                                                                                                                                                                                                                                               
---------------------------------------------------------------------------
DeprecationWarning                        Traceback (most recent call last)
<ipython-input-4-7e899118b3ad> in <module>
----> 1 dates[:, None]

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/extension.py in __getitem__(self, key)
    179 
    180         # Includes cases where we get a 2D ndarray back for MPL compat
--> 181         deprecate_ndim_indexing(result)
    182         return result
    183 

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in deprecate_ndim_indexing(result)
   5546         #  cannot do that and keep an index, so return ndarray
   5547         # Deprecation GH#30588
-> 5548         warnings.warn(
   5549             "Support for multi-dimensional indexing (e.g. `index[:, None]`) "
   5550             "on an Index is deprecated and will be removed in a future "

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.

In [5]: x.index[:, None]                                                                                                                                                                                                                                                                             
---------------------------------------------------------------------------
DeprecationWarning                        Traceback (most recent call last)
<ipython-input-5-dc66658d63da> in <module>
----> 1 x.index[:, None]

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/range.py in __getitem__(self, key)
    706             )
    707         # fall back to Int64Index
--> 708         return super().__getitem__(key)
    709 
    710     @unpack_zerodim_and_defer("__floordiv__")

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in __getitem__(self, key)
   3906         if not is_scalar(result):
   3907             if np.ndim(result) > 1:
-> 3908                 deprecate_ndim_indexing(result)
   3909                 return result
   3910             return promote(result)

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in deprecate_ndim_indexing(result)
   5546         #  cannot do that and keep an index, so return ndarray
   5547         # Deprecation GH#30588
-> 5548         warnings.warn(
   5549             "Support for multi-dimensional indexing (e.g. `index[:, None]`) "
   5550             "on an Index is deprecated and will be removed in a future "

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.

In [6]: warnings.filterwarnings("always", 
   ...:                                         category=DeprecationWarning, 
   ...:                                         module='pandas[.*]')                                                                                                                                                                                                                                 

In [7]: x.index[:, None]                                                                                                                                                                                                                                                                             
/home/tcaswell/.virtualenvs/sys38/lib/python3.8/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)
Out[7]: array([], shape=(0, 1), dtype=int64)

In [8]: dates[:, None]                                                                                                                                                                                                                                                                               
---------------------------------------------------------------------------
DeprecationWarning                        Traceback (most recent call last)
<ipython-input-8-7e899118b3ad> in <module>
----> 1 dates[:, None]

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/extension.py in __getitem__(self, key)
    179 
    180         # Includes cases where we get a 2D ndarray back for MPL compat
--> 181         deprecate_ndim_indexing(result)
    182         return result
    183 

~/.virtualenvs/sys38/lib/python3.8/site-packages/pandas/core/indexes/base.py in deprecate_ndim_indexing(result)
   5546         #  cannot do that and keep an index, so return ndarray
   5547         # Deprecation GH#30588
-> 5548         warnings.warn(
   5549             "Support for multi-dimensional indexing (e.g. `index[:, None]`) "
   5550             "on an Index is deprecated and will be removed in a future "

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.

In [9]: type(dates)                                                                                                                                                                                                                                                                                  
Out[9]: pandas.core.indexes.datetimes.DatetimeIndex

In [10]: type(x.index)                                                                                                                                                                                                                                                                               
Out[10]: pandas.core.indexes.range.RangeIndex

In [11]:       

In [12]: warnings.simplefilter('always')                                                                                                                                                                                                                                                             

In [13]: type(dates)                                                                                                                                                                                                                                                                                 
Out[13]: pandas.core.indexes.datetimes.DatetimeIndex

In [14]: dates[:, None]                                                                                                                                                                                                                                                                              
<ipython-input-14-7e899118b3ad>:1: 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]
Out[14]: 
array([['2018-01-01T00:00:00.000000000'],
       ['2018-01-02T00:00:00.000000000'],
       ['2018-01-03T00:00:00.000000000']], dtype='datetime64[ns]')

In [15]: x.index[:, None]                                                                                                                                                                                                                                                                            
/home/tcaswell/.virtualenvs/sys38/lib/python3.8/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)
Out[15]: array([], shape=(0, 1), dtype=int64)

In [16]:                                       

@jklymak
Copy link
Member

jklymak commented Jan 31, 2020

Glad you could track it down; I was sure scratching my head....

@tacaswell
Copy link
Member

On a bit more consideration, I think that the behaviour I was using before is actually the buggy behavior and the one that re-broke us was behaving correctly. It makes sense for pandas to set the stack level to warn where they are being called incorrectly, not where inside of them selves they are letting you know you are using them wrong.

@tacaswell
Copy link
Member

xref pandas-dev/pandas#31479

jklymak pushed a commit to dstansby/matplotlib that referenced this issue Feb 3, 2020
Not all of the warnings from pandas report that they are from pandas.

Closes matplotlib#16295 (again)
jklymak pushed a commit to jklymak/matplotlib that referenced this issue Feb 3, 2020
Not all of the warnings from pandas report that they are from pandas.

Closes matplotlib#16295 (again)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
4 participants