Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support masked dates #24734

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
Dec 16, 2022
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
4 changes: 4 additions & 0 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ def date2num(d):
if not iterable:
d = [d]

masked = np.ma.is_masked(d)
mask = np.ma.getmask(d)
d = np.asarray(d)

# convert to datetime64 arrays, if not already:
if not np.issubdtype(d.dtype, np.datetime64):
# datetime arrays
Expand All @@ -458,6 +461,7 @@ def date2num(d):
d = np.asarray(d)
d = d.astype('datetime64[us]')

d = np.ma.masked_array(d, mask=mask) if masked else d
d = _dt64_to_ordinalf(d)

return d if iterable else d[0]
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ def test_date2num_NaT_scalar(units):
assert np.isnan(tmpl)


def test_date2num_masked():
# Without tzinfo
base = datetime.datetime(2022, 12, 15)
dates = np.ma.array([base + datetime.timedelta(days=(2 * i))
for i in range(7)], mask=[0, 1, 1, 0, 0, 0, 1])
npdates = mdates.date2num(dates)
np.testing.assert_array_equal(np.ma.getmask(npdates),
(False, True, True, False, False, False,
True))

# With tzinfo
base = datetime.datetime(2022, 12, 15, tzinfo=mdates.UTC)
dates = np.ma.array([base + datetime.timedelta(days=(2 * i))
for i in range(7)], mask=[0, 1, 1, 0, 0, 0, 1])
npdates = mdates.date2num(dates)
np.testing.assert_array_equal(np.ma.getmask(npdates),
(False, True, True, False, False, False,
True))


def test_date_empty():
# make sure we do the right thing when told to plot dates even
# if no date data has been presented, cf
Expand Down