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

Skip to content

Manual backport of pr 17983 on v3.3.x #17990

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
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
9 changes: 9 additions & 0 deletions doc/api/api_changes_3.4/deprecations.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
Deprecations
------------

Reverted deprecation of `~.dates.num2epoch` and `~.dates.epoch2num`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These two functions were deprecated in 3.3.0, and did not return
an accurate Matplotlib datenum relative to the new Matplotlib epoch
handling (`~.dates.get_epoch` and :rc:`date.epoch`). This version
reverts the deprecation and fixes those functions to work with
`~.dates.get_epoch`.
38 changes: 31 additions & 7 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,21 +1752,45 @@ def _get_interval(self):
return self._interval


@cbook.deprecated("3.3")
def epoch2num(e):
"""
Convert an epoch or sequence of epochs to the new date format,
that is days since 0001.
Convert UNIX time to days since Matplotlib epoch.

Parameters
----------
e : list of floats
Time in seconds since 1970-01-01.

Returns
-------
`numpy.array`
Time in days since Matplotlib epoch (see `~.dates.get_epoch()`).
"""
return EPOCH_OFFSET + np.asarray(e) / SEC_PER_DAY

dt = (np.datetime64('1970-01-01T00:00:00', 's') -
np.datetime64(get_epoch(), 's')).astype(float)

return (dt + np.asarray(e)) / SEC_PER_DAY


@cbook.deprecated("3.3")
def num2epoch(d):
"""
Convert days since 0001 to epoch. *d* can be a number or sequence.
Convert days since Matplotlib epoch to UNIX time.

Parameters
----------
d : list of floats
Time in days since Matplotlib epoch (see `~.dates.get_epoch()`).

Returns
-------
`numpy.array`
Time in seconds since 1970-01-01.
"""
return (np.asarray(d) - EPOCH_OFFSET) * SEC_PER_DAY
dt = (np.datetime64('1970-01-01T00:00:00', 's') -
np.datetime64(get_epoch(), 's')).astype(float)

return np.asarray(d) * SEC_PER_DAY - dt


@cbook.deprecated("3.2")
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,15 @@ def test_change_epoch():
np.testing.assert_allclose(
mdates.date2num(np.datetime64('1970-01-01T12:00:00')),
0.5)


def test_epoch2num():
mdates._reset_epoch_test_example()
mdates.set_epoch('0000-12-31')
assert mdates.epoch2num(86400) == 719164.0
assert mdates.num2epoch(719165.0) == 86400 * 2
# set back to the default
mdates._reset_epoch_test_example()
mdates.set_epoch('1970-01-01T00:00:00')
assert mdates.epoch2num(86400) == 1.0
assert mdates.num2epoch(2.0) == 86400 * 2