Skip to content

[Bug]: axes.set_xlim with string dates raises when plotting with datetimes #25219

Closed
@mroeschke

Description

@mroeschke

Bug summary

pandas is seeing recent failures with the 3.7.0 release when using set_xlim with strings representing dates and plotting on the x axis with dates

Code for reproduction

In [1]: import matplotlib.pyplot as plt

In [2]: dates = np.array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000'], dtype='datetime64[ns]')

In [3]: values = np.arange(2)

In [4]: _, ax = plt.subplots()

In [5]: ax.plot(dates, values)
Out[5]: [<matplotlib.lines.Line2D at 0x7fba780ae130>]

In [6]: ax.set_xlim("1/1/1999", "1/1/2001")
Traceback (most recent call last):
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/axis.py", line 1736, in convert_units
    ret = self.converter.convert(x, self.units, self)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/category.py", line 57, in convert
    unit.update(values)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/category.py", line 216, in update
    _api.check_isinstance((str, bytes), value=val)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/_api/__init__.py", line 93, in check_isinstance
    raise TypeError(
TypeError: 'value' must be an instance of str or bytes, not a int

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 304, in process
    func(*args, **kwargs)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2455, in _unit_change_handler
    line.recache_always()
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/lines.py", line 652, in recache_always
    self.recache(always=True)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/lines.py", line 656, in recache
    xconv = self.convert_xunits(self._xorig)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/artist.py", line 281, in convert_xunits
    return ax.xaxis.convert_units(x)
  File "/Users/matthewroeschke/opt/miniconda3/envs/pandas-dev/lib/python3.8/site-packages/matplotlib/axis.py", line 1738, in convert_units
    raise munits.ConversionError('Failed to convert value(s) to axis '
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000'],
      dtype='datetime64[ns]')
Out[6]: (0.0, 1.0)

In [7]: import matplotlib; matplotlib.__version__
Out[7]: '3.7.0'

Actual outcome

(The iPython example above shows the outcome)

Expected outcome

No exception traceback.

Additional information

This appeared to work in the 3.6.x series. I could no find documentation regarding this functionality not being supported

Operating system

No response

Matplotlib Version

3.7.0

Matplotlib Backend

No response

Python version

3.8

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions