Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60dd992

Browse filesBrowse files
authored
Merge pull request #24734 from oscargus/maskeddates
Support masked dates
2 parents 7f5335a + 2353884 commit 60dd992
Copy full SHA for 60dd992

File tree

Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed

‎lib/matplotlib/dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/dates.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def date2num(d):
444444
if not iterable:
445445
d = [d]
446446

447+
masked = np.ma.is_masked(d)
448+
mask = np.ma.getmask(d)
447449
d = np.asarray(d)
450+
448451
# convert to datetime64 arrays, if not already:
449452
if not np.issubdtype(d.dtype, np.datetime64):
450453
# datetime arrays
@@ -458,6 +461,7 @@ def date2num(d):
458461
d = np.asarray(d)
459462
d = d.astype('datetime64[us]')
460463

464+
d = np.ma.masked_array(d, mask=mask) if masked else d
461465
d = _dt64_to_ordinalf(d)
462466

463467
return d if iterable else d[0]

‎lib/matplotlib/tests/test_dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_dates.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ def test_date2num_NaT_scalar(units):
6969
assert np.isnan(tmpl)
7070

7171

72+
def test_date2num_masked():
73+
# Without tzinfo
74+
base = datetime.datetime(2022, 12, 15)
75+
dates = np.ma.array([base + datetime.timedelta(days=(2 * i))
76+
for i in range(7)], mask=[0, 1, 1, 0, 0, 0, 1])
77+
npdates = mdates.date2num(dates)
78+
np.testing.assert_array_equal(np.ma.getmask(npdates),
79+
(False, True, True, False, False, False,
80+
True))
81+
82+
# With tzinfo
83+
base = datetime.datetime(2022, 12, 15, tzinfo=mdates.UTC)
84+
dates = np.ma.array([base + datetime.timedelta(days=(2 * i))
85+
for i in range(7)], mask=[0, 1, 1, 0, 0, 0, 1])
86+
npdates = mdates.date2num(dates)
87+
np.testing.assert_array_equal(np.ma.getmask(npdates),
88+
(False, True, True, False, False, False,
89+
True))
90+
91+
7292
def test_date_empty():
7393
# make sure we do the right thing when told to plot dates even
7494
# if no date data has been presented, cf

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.