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

Skip to content

Commit ac6aa6e

Browse files
authored
Merge pull request #17990 from jklymak/auto-backport-of-pr-17983-on-v3.3.x
Manual backport of pr 17983 on v3.3.x
2 parents 8272171 + b50fa14 commit ac6aa6e

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
Deprecations
22
------------
3+
4+
Reverted deprecation of `~.dates.num2epoch` and `~.dates.epoch2num`
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
7+
These two functions were deprecated in 3.3.0, and did not return
8+
an accurate Matplotlib datenum relative to the new Matplotlib epoch
9+
handling (`~.dates.get_epoch` and :rc:`date.epoch`). This version
10+
reverts the deprecation and fixes those functions to work with
11+
`~.dates.get_epoch`.

lib/matplotlib/dates.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,21 +1752,45 @@ def _get_interval(self):
17521752
return self._interval
17531753

17541754

1755-
@cbook.deprecated("3.3")
17561755
def epoch2num(e):
17571756
"""
1758-
Convert an epoch or sequence of epochs to the new date format,
1759-
that is days since 0001.
1757+
Convert UNIX time to days since Matplotlib epoch.
1758+
1759+
Parameters
1760+
----------
1761+
e : list of floats
1762+
Time in seconds since 1970-01-01.
1763+
1764+
Returns
1765+
-------
1766+
`numpy.array`
1767+
Time in days since Matplotlib epoch (see `~.dates.get_epoch()`).
17601768
"""
1761-
return EPOCH_OFFSET + np.asarray(e) / SEC_PER_DAY
1769+
1770+
dt = (np.datetime64('1970-01-01T00:00:00', 's') -
1771+
np.datetime64(get_epoch(), 's')).astype(float)
1772+
1773+
return (dt + np.asarray(e)) / SEC_PER_DAY
17621774

17631775

1764-
@cbook.deprecated("3.3")
17651776
def num2epoch(d):
17661777
"""
1767-
Convert days since 0001 to epoch. *d* can be a number or sequence.
1778+
Convert days since Matplotlib epoch to UNIX time.
1779+
1780+
Parameters
1781+
----------
1782+
d : list of floats
1783+
Time in days since Matplotlib epoch (see `~.dates.get_epoch()`).
1784+
1785+
Returns
1786+
-------
1787+
`numpy.array`
1788+
Time in seconds since 1970-01-01.
17681789
"""
1769-
return (np.asarray(d) - EPOCH_OFFSET) * SEC_PER_DAY
1790+
dt = (np.datetime64('1970-01-01T00:00:00', 's') -
1791+
np.datetime64(get_epoch(), 's')).astype(float)
1792+
1793+
return np.asarray(d) * SEC_PER_DAY - dt
17701794

17711795

17721796
@cbook.deprecated("3.2")

lib/matplotlib/tests/test_dates.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,15 @@ def test_change_epoch():
947947
np.testing.assert_allclose(
948948
mdates.date2num(np.datetime64('1970-01-01T12:00:00')),
949949
0.5)
950+
951+
952+
def test_epoch2num():
953+
mdates._reset_epoch_test_example()
954+
mdates.set_epoch('0000-12-31')
955+
assert mdates.epoch2num(86400) == 719164.0
956+
assert mdates.num2epoch(719165.0) == 86400 * 2
957+
# set back to the default
958+
mdates._reset_epoch_test_example()
959+
mdates.set_epoch('1970-01-01T00:00:00')
960+
assert mdates.epoch2num(86400) == 1.0
961+
assert mdates.num2epoch(2.0) == 86400 * 2

0 commit comments

Comments
 (0)