FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support masked dates by oscargus · Pull Request #24734 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions lib/matplotlib/dates.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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]')
Comment thread
oscargus marked this conversation as resolved.

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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))
Comment thread
rcomer marked this conversation as resolved.

# 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

Back | FazBrowse Home | New Git URL