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

Skip to content

Commit 669e1cc

Browse files
committed
MNT: deprecate epoch2num/num2epoch
1 parent 8fda099 commit 669e1cc

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``epoch2num`` and ``num2epoch`` are deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
These methods convert from unix timestamps to matplotlib floats, but are not
4+
useed internally to matplotlib, and should not be needed by endusers.
5+
To convert a unix timestamp to datetime, simply use
6+
`datetime.datetime.utcfromtimestamp`, or to use numpy datetime64
7+
``dt = np.datetim64(e*1e6, 'us')``.

lib/matplotlib/dates.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,8 @@ def _get_interval(self):
17291729
return self._interval
17301730

17311731

1732+
@_api.deprecated("3.5",
1733+
alternative="mdates.date2num(datetime.utcfromtimestamp(e))")
17321734
def epoch2num(e):
17331735
"""
17341736
Convert UNIX time to days since Matplotlib epoch.
@@ -1750,6 +1752,7 @@ def epoch2num(e):
17501752
return (dt + np.asarray(e)) / SEC_PER_DAY
17511753

17521754

1755+
@_api.deprecated("3.5", alternative="mdates.num2date(e).timestamp()")
17531756
def num2epoch(d):
17541757
"""
17551758
Convert days since Matplotlib epoch to UNIX time.

lib/matplotlib/tests/test_dates.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import matplotlib.pyplot as plt
1212
from matplotlib.testing.decorators import image_comparison
1313
import matplotlib.ticker as mticker
14+
import matplotlib._api as _api
1415

1516

1617
def test_date_numpyx():
@@ -1078,18 +1079,6 @@ def test_change_interval_multiples():
10781079
assert ax.get_xticklabels()[1].get_text() == 'Feb 01 2020'
10791080

10801081

1081-
def test_epoch2num():
1082-
mdates._reset_epoch_test_example()
1083-
mdates.set_epoch('0000-12-31')
1084-
assert mdates.epoch2num(86400) == 719164.0
1085-
assert mdates.num2epoch(719165.0) == 86400 * 2
1086-
# set back to the default
1087-
mdates._reset_epoch_test_example()
1088-
mdates.set_epoch('1970-01-01T00:00:00')
1089-
assert mdates.epoch2num(86400) == 1.0
1090-
assert mdates.num2epoch(2.0) == 86400 * 2
1091-
1092-
10931082
def test_julian2num():
10941083
mdates._reset_epoch_test_example()
10951084
mdates.set_epoch('0000-12-31')
@@ -1102,3 +1091,16 @@ def test_julian2num():
11021091
mdates.set_epoch('1970-01-01T00:00:00')
11031092
assert mdates.julian2num(2440588.5) == 1.0
11041093
assert mdates.num2julian(2.0) == 2440589.5
1094+
1095+
1096+
def test_epoch2num():
1097+
with _api.suppress_matplotlib_deprecation_warning():
1098+
mdates._reset_epoch_test_example()
1099+
mdates.set_epoch('0000-12-31')
1100+
assert mdates.epoch2num(86400) == 719164.0
1101+
assert mdates.num2epoch(719165.0) == 86400 * 2
1102+
# set back to the default
1103+
mdates._reset_epoch_test_example()
1104+
mdates.set_epoch('1970-01-01T00:00:00')
1105+
assert mdates.epoch2num(86400) == 1.0
1106+
assert mdates.num2epoch(2.0) == 86400 * 2

0 commit comments

Comments
 (0)