def test_change_epoch(): date = np.datetime64('2000-01-01') # use private method to clear the epoch and allow it to be set... mdates._reset_epoch_test_example() mdates.get_epoch() # Set default. with pytest.raises(RuntimeError): # this should fail here because there is a sentinel on the epoch # if the epoch has been used then it cannot be set. mdates.set_epoch('0000-01-01') mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01') dt = (date - np.datetime64('1970-01-01')).astype('datetime64[D]') dt = dt.astype('int') np.testing.assert_equal(mdates.date2num(date), float(dt)) mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') np.testing.assert_equal(mdates.date2num(date), 730120.0) mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T01:00:00') np.testing.assert_allclose(mdates.date2num(date), dt - 1. / 24.) mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') np.testing.assert_allclose( mdates.date2num(np.datetime64('1970-01-01T12:00:00')), 0.5)
mdates.set_epoch(old_epoch) x = np.arange('2000-01-01T00:00:00.0', '2000-01-01T00:00:00.000100', dtype='datetime64[us]') # simulate the plot being made using the old epoch xold = np.array([mdates.num2date(mdates.date2num(d)) for d in x]) y = np.arange(0, len(x)) # resetting the Epoch so plots are comparable _reset_epoch_for_tutorial() # Don't do this. Just for this tutorial. mdates.set_epoch(new_epoch) fig, ax = plt.subplots(constrained_layout=True) ax.plot(xold, y) ax.set_title('Epoch: ' + mdates.get_epoch()) plt.setp(ax.xaxis.get_majorticklabels(), rotation=40) plt.show() ############################################################################# # For dates plotted using the more recent epoch, the plot is smooth: fig, ax = plt.subplots(constrained_layout=True) ax.plot(x, y) ax.set_title('Epoch: ' + mdates.get_epoch()) plt.setp(ax.xaxis.get_majorticklabels(), rotation=40) plt.show() _reset_epoch_for_tutorial() # Don't do this. Just for this tutorial. #############################################################################