-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
pyplot.plot using pandas series raises DeprecationWarning with pandas=1.0.0rc0 #16295
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
Comments
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. |
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
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
Re-opening because the changes in #16347 don't seem to capture the warning? |
Not all of the warnings from pandas report that they are from pandas. Closes matplotlib#16295 (again)
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 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]: |
Glad you could track it down; I was sure scratching my head.... |
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. |
Not all of the warnings from pandas report that they are from pandas. Closes matplotlib#16295 (again)
Not all of the warnings from pandas report that they are from pandas. Closes matplotlib#16295 (again)
Bug report
Bug summary
The code below raises
DeprecationWarning
frompandas=1.0.0rc0
whenplt.plot
is fed with twopandas.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
Actual outcome
Expected outcome
No warnings.
Matplotlib version
print(matplotlib.get_backend())
): aggpython and matplotlib obtained through anaconda, pandas through pip
The text was updated successfully, but these errors were encountered: