Skip to content

X-axis that is Pandas Series time zone aware timestamps raises AssertionError #18158

Closed
@l-johnston

Description

@l-johnston

Bug report

Bug summary

Attempting to plot with an x-axis that is a Pandas Series object of time zone aware Timestamps raises AssertionError: <class 'numpy.ndarray'>.

Code for reproduction

import datetime
import pandas as pd
import matplotlib.pyplot as plt
x = pd.Series([datetime.datetime.fromisoformat(f"2020-01-01T00:00:0{s}").astimezone() for s in range(3)])
y = [0,1,2]
plt.plot(x, y)

Actual outcome

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-13-d0d1dfa5badf> in <module>
----> 1 plt.plot(x, y)

~/python/lib/python3.8/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2821 @_copy_docstring_and_deprecators(Axes.plot)
   2822 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2823     return gca().plot(
   2824         *args, scalex=scalex, scaley=scaley,
   2825         **({"data": data} if data is not None else {}), **kwargs)

~/python/lib/python3.8/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

~/python/lib/python3.8/site-packages/matplotlib/axes/_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274
    275     def get_next_color(self):

~/python/lib/python3.8/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    386
    387         if len(tup) == 2:
--> 388             x = _check_1d(tup[0])
    389             y = _check_1d(tup[-1])
    390         else:

~/python/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _check_1d(x)
   1316                     message='Support for multi-dimensional indexing')
   1317
-> 1318                 ndim = x[:, None].ndim
   1319                 # we have definitely hit a pandas index or series object
   1320                 # cast to a numpy array.

~/python/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
    906             return self._get_values(key)
    907
--> 908         return self._get_with(key)
    909
    910     def _get_with(self, key):

~/python/lib/python3.8/site-packages/pandas/core/series.py in _get_with(self, key)
    921             )
    922         elif isinstance(key, tuple):
--> 923             return self._get_values_tuple(key)
    924
    925         elif not is_list_like(key):

~/python/lib/python3.8/site-packages/pandas/core/series.py in _get_values_tuple(self, key)
    951         # mpl hackaround
    952         if com.any_none(*key):
--> 953             result = self._get_values(key)
    954             deprecate_ndim_indexing(result, stacklevel=5)
    955             return result

~/python/lib/python3.8/site-packages/pandas/core/series.py in _get_values(self, indexer)
    966     def _get_values(self, indexer):
    967         try:
--> 968             return self._constructor(self._mgr.get_slice(indexer)).__finalize__(self,)
    969         except ValueError:
    970             # mpl compat if we look up e.g. ser[:, np.newaxis];

~/python/lib/python3.8/site-packages/pandas/core/internals/managers.py in get_slice(self, slobj, axis)
   1561         array = blk._slice(slobj)
   1562         block = blk.make_block_same_class(array, placement=slice(0, len(array)))
-> 1563         return type(self)(block, self.index[slobj])
   1564
   1565     @property

~/python/lib/python3.8/site-packages/pandas/core/internals/managers.py in __init__(self, block, axis, do_integrity_check, fastpath)
   1505     ):
   1506         assert isinstance(block, Block), type(block)
-> 1507         assert isinstance(axis, Index), type(axis)
   1508
   1509         if fastpath is not lib.no_default:

AssertionError: <class 'numpy.ndarray'>

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.3.0
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.5
  • Pandas: 1.1.0

The problem is in /matplotlib/cbook/init.py line 1318 multi-dimensional indexing that is now deprecated in Pandas. This issue is handled for some Series objects but not x-axis time zone aware timestamps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions