| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -444,7 +444,10 @@ def date2num(d): | |||
| 444 | 444 | if not iterable: | |
| 445 | 445 | d = [d] | |
| 446 | 446 | ||
| 447 | + masked = np.ma.is_masked(d) | ||
| 448 | + mask = np.ma.getmask(d) | ||
| 447 | 449 | d = np.asarray(d) | |
| 450 | + | ||
| 448 | 451 | # convert to datetime64 arrays, if not already: | |
| 449 | 452 | if not np.issubdtype(d.dtype, np.datetime64): | |
| 450 | 453 | # datetime arrays | |
@@ -458,6 +461,7 @@ def date2num(d): | |||
| 458 | 461 | d = np.asarray(d) | |
| 459 | 462 | d = d.astype('datetime64[us]') | |
| 460 | 463 | ||
| 464 | + d = np.ma.masked_array(d, mask=mask) if masked else d | ||
| 461 | 465 | d = _dt64_to_ordinalf(d) | |
| 462 | 466 | ||
| 463 | 467 | return d if iterable else d[0] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,6 +69,26 @@ def test_date2num_NaT_scalar(units): | |||
| 69 | 69 | assert np.isnan(tmpl) | |
| 70 | 70 | ||
| 71 | 71 | ||
| 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 | + | ||
| 72 | 92 | def test_date_empty(): | |
| 73 | 93 | # make sure we do the right thing when told to plot dates even | |
| 74 | 94 | # if no date data has been presented, cf | |
| Back | FazBrowse Home | New Git URL |
0 commit comments