diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 02c54f28a1695d..185d6b7d3ae83e 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -1643,6 +1643,7 @@ Plotting - Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`) - Bug in the date and time converters pandas registers with matplotlib not handling multiple dimensions (:issue:`16026`) - Bug in ``pd.scatter_matrix()`` could accept either ``color`` or ``c``, but not both (:issue:`14855`) +- Bug in ``plot`` where ``NaT`` in ``DatetimeIndex`` results in ``Timestamp.min`` (:issue: `12405`) Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index c3476d1443fc3a..389cd1c926ab35 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -11,6 +11,7 @@ from pandas.util.decorators import cache_readonly from pandas.core.base import PandasObject +from pandas.core.dtypes.missing import notnull from pandas.core.dtypes.common import ( is_list_like, is_integer, @@ -537,6 +538,7 @@ def _get_xticks(self, convert_period=False): """ x = index._mpl_repr() elif is_datetype: + self.data = self.data[notnull(self.data.index)] self.data = self.data.sort_index() x = self.data.index._mpl_repr() else: diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 803907c60d0d0b..be30690c4f5d61 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -6,7 +6,7 @@ from pandas.compat import lrange, zip import numpy as np -from pandas import Index, Series, DataFrame +from pandas import Index, Series, DataFrame, NaT from pandas.compat import is_platform_mac from pandas.core.indexes.datetimes import date_range, bdate_range from pandas.core.indexes.timedeltas import timedelta_range @@ -815,6 +815,23 @@ def test_mixed_freq_shared_ax(self): # self.assertEqual(ax1.lines[0].get_xydata()[0, 0], # ax2.lines[0].get_xydata()[0, 0]) + def test_nat_handling(self): + + import matplotlib.pyplot as plt # noqa + + fig = plt.gcf() + plt.clf() + ax = fig.add_subplot(111) + + dti = DatetimeIndex(['2015-01-01', NaT, '2015-01-03']) + s = Series(range(len(dti)), dti) + s.plot(ax=ax) + xdata = ax.get_lines()[0].get_xdata() + # plot x data is bounded by index values + self.assertLessEqual(s.index.min(), Series(xdata).min()) + self.assertLessEqual(Series(xdata).max(), s.index.max()) + _check_plot_works(s.plot) + @slow def test_to_weekly_resampling(self): idxh = date_range('1/1/1999', periods=52, freq='W')