Skip to content

Commit a6fcec6

Browse files
tsdlovellTomAugspurger
authored andcommitted
BUG: fixes #12405 by eliding values index by NaT in MPLPlot._get_xticks (#14540)
TST: add test for fix of #12405 DOC: update whatsnew/v0.20.2.txt
1 parent 0f55de1 commit a6fcec6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Plotting
6060
^^^^^^^^
6161

6262
- Bug in ``DataFrame.plot`` with a single column and a list-like ``color`` (:issue:`3486`)
63+
- Bug in ``plot`` where ``NaT`` in ``DatetimeIndex`` results in ``Timestamp.min`` (:issue: `12405`)
6364

6465

6566

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,
@@ -538,6 +539,7 @@ def _get_xticks(self, convert_period=False):
538539
"""
539540
x = index._mpl_repr()
540541
elif is_datetype:
542+
self.data = self.data[notnull(self.data.index)]
541543
self.data = self.data.sort_index()
542544
x = self.data.index._mpl_repr()
543545
else:

pandas/tests/plotting/test_datetimelike.py

+15-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
@@ -811,6 +811,20 @@ def test_mixed_freq_shared_ax(self):
811811
# assert (ax1.lines[0].get_xydata()[0, 0] ==
812812
# ax2.lines[0].get_xydata()[0, 0])
813813

814+
def test_nat_handling(self):
815+
816+
fig = self.plt.gcf()
817+
# self.plt.clf()
818+
ax = fig.add_subplot(111)
819+
820+
dti = DatetimeIndex(['2015-01-01', NaT, '2015-01-03'])
821+
s = Series(range(len(dti)), dti)
822+
s.plot(ax=ax)
823+
xdata = ax.get_lines()[0].get_xdata()
824+
# plot x data is bounded by index values
825+
assert s.index.min() <= Series(xdata).min()
826+
assert Series(xdata).max() <= s.index.max()
827+
814828
@slow
815829
def test_to_weekly_resampling(self):
816830
idxh = date_range('1/1/1999', periods=52, freq='W')

0 commit comments

Comments
 (0)