Skip to content

Commit 1c2cad6

Browse files
committed
BUG: fixes pandas-dev#12405 by eliding values index by NaT in MPLPlot._get_xticks
TST: add test for fix of pandas-dev#12405 DOC: update whatsnew/v0.20.0.txt
1 parent 1f16ca7 commit 1c2cad6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,7 @@ Plotting
16431643
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
16441644
- Bug in the date and time converters pandas registers with matplotlib not handling multiple dimensions (:issue:`16026`)
16451645
- Bug in ``pd.scatter_matrix()`` could accept either ``color`` or ``c``, but not both (:issue:`14855`)
1646+
- Bug in ``plot`` where ``NaT`` in ``DatetimeIndex`` results in ``Timestamp.min`` (:issue: `12405`)
16461647

16471648
Groupby/Resample/Rolling
16481649
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_core.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pandas.util.decorators import cache_readonly
1313
from pandas.core.base import PandasObject
14+
from pandas.core.dtypes.missing import notnull
1415
from pandas.core.dtypes.common import (
1516
is_list_like,
1617
is_integer,
@@ -537,6 +538,7 @@ def _get_xticks(self, convert_period=False):
537538
"""
538539
x = index._mpl_repr()
539540
elif is_datetype:
541+
self.data = self.data[notnull(self.data.index)]
540542
self.data = self.data.sort_index()
541543
x = self.data.index._mpl_repr()
542544
else:

pandas/tests/plotting/test_datetimelike.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas.compat import lrange, zip
77

88
import numpy as np
9-
from pandas import Index, Series, DataFrame
9+
from pandas import Index, Series, DataFrame, NaT
1010
from pandas.compat import is_platform_mac
1111
from pandas.core.indexes.datetimes import date_range, bdate_range
1212
from pandas.core.indexes.timedeltas import timedelta_range
@@ -815,6 +815,23 @@ def test_mixed_freq_shared_ax(self):
815815
# self.assertEqual(ax1.lines[0].get_xydata()[0, 0],
816816
# ax2.lines[0].get_xydata()[0, 0])
817817

818+
def test_nat_handling(self):
819+
820+
import matplotlib.pyplot as plt # noqa
821+
822+
fig = plt.gcf()
823+
plt.clf()
824+
ax = fig.add_subplot(111)
825+
826+
dti = DatetimeIndex(['2015-01-01', NaT, '2015-01-03'])
827+
s = Series(range(len(dti)), dti)
828+
s.plot(ax=ax)
829+
xdata = ax.get_lines()[0].get_xdata()
830+
# plot x data is bounded by index values
831+
self.assertLessEqual(s.index.min(), Series(xdata).min())
832+
self.assertLessEqual(Series(xdata).max(), s.index.max())
833+
_check_plot_works(s.plot)
834+
818835
@slow
819836
def test_to_weekly_resampling(self):
820837
idxh = date_range('1/1/1999', periods=52, freq='W')

0 commit comments

Comments
 (0)