Skip to content

Convert NaT to nan in date2num #10631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ def _dt64_to_ordinalf(d):
dt += extra.astype(np.float64) / 1.0e9
dt = dt / SEC_PER_DAY + 1.0

NaT_int = np.datetime64('NaT').astype(np.int64)
d_int = d.astype(np.int64)
try:
dt[d_int == NaT_int] = np.nan
except TypeError:
if d_int == NaT_int:
dt = np.nan
return dt


Expand Down
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,33 @@ def test_date_numpyx():
datetime.datetime(2017, 1, 1, 3, 1, 1)]]])
@pytest.mark.parametrize('dtype', ['datetime64[s]',
'datetime64[us]',
'datetime64[ms]'])
'datetime64[ms]',
'datetime64[ns]'])
def test_date_date2num_numpy(t0, dtype):
time = mdates.date2num(t0)
tnp = np.array(t0, dtype=dtype)
nptime = mdates.date2num(tnp)
assert np.array_equal(time, nptime)


@pytest.mark.parametrize('dtype', ['datetime64[s]',
'datetime64[us]',
'datetime64[ms]',
'datetime64[ns]'])
def test_date2num_NaT(dtype):
t0 = datetime.datetime(2017, 1, 1, 0, 1, 1)
tmpl = [mdates.date2num(t0), np.nan]
tnp = np.array([t0, 'NaT'], dtype=dtype)
nptime = mdates.date2num(tnp)
np.testing.assert_array_equal(tmpl, nptime)


@pytest.mark.parametrize('units', ['s', 'ms', 'us', 'ns'])
def test_date2num_NaT_scalar(units):
tmpl = mdates.date2num(np.datetime64('NaT', units))
assert np.isnan(tmpl)


@image_comparison(baseline_images=['date_empty'], extensions=['png'])
def test_date_empty():
# make sure mpl does the right thing when told to plot dates even
Expand Down