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

Skip to content

Commit ef7feb1

Browse files
QuLogicjklymak
authored andcommitted
Merge pull request #17983 from jklymak/fix-pandas-need-num2epoch
FIX: undeprecate and update num2epoch/epoch2num
1 parent 8272171 commit ef7feb1

2 files changed

Lines changed: 94 additions & 7 deletions

File tree

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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,66 @@ 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_warn_notintervals():
953+
dates = np.arange('2001-01-10', '2001-03-04', dtype='datetime64[D]')
954+
locator = mdates.AutoDateLocator(interval_multiples=False)
955+
locator.intervald[3] = [2]
956+
locator.create_dummy_axis()
957+
locator.set_view_interval(mdates.date2num(dates[0]),
958+
mdates.date2num(dates[-1]))
959+
with pytest.warns(UserWarning, match="AutoDateLocator was unable") as rec:
960+
locs = locator()
961+
962+
963+
def test_change_converter():
964+
plt.rcParams['date.converter'] = 'concise'
965+
dates = np.arange('2020-01-01', '2020-05-01', dtype='datetime64[D]')
966+
fig, ax = plt.subplots()
967+
968+
ax.plot(dates, np.arange(len(dates)))
969+
fig.canvas.draw()
970+
assert ax.get_xticklabels()[0].get_text() == 'Jan'
971+
assert ax.get_xticklabels()[1].get_text() == '15'
972+
973+
plt.rcParams['date.converter'] = 'auto'
974+
fig, ax = plt.subplots()
975+
976+
ax.plot(dates, np.arange(len(dates)))
977+
fig.canvas.draw()
978+
assert ax.get_xticklabels()[0].get_text() == 'Jan 01 2020'
979+
assert ax.get_xticklabels()[1].get_text() == 'Jan 15 2020'
980+
with pytest.warns(UserWarning) as rec:
981+
plt.rcParams['date.converter'] = 'boo'
982+
983+
984+
def test_change_interval_multiples():
985+
plt.rcParams['date.interval_multiples'] = False
986+
dates = np.arange('2020-01-10', '2020-05-01', dtype='datetime64[D]')
987+
fig, ax = plt.subplots()
988+
989+
ax.plot(dates, np.arange(len(dates)))
990+
fig.canvas.draw()
991+
assert ax.get_xticklabels()[0].get_text() == 'Jan 10 2020'
992+
assert ax.get_xticklabels()[1].get_text() == 'Jan 24 2020'
993+
994+
plt.rcParams['date.interval_multiples'] = 'True'
995+
fig, ax = plt.subplots()
996+
997+
ax.plot(dates, np.arange(len(dates)))
998+
fig.canvas.draw()
999+
assert ax.get_xticklabels()[0].get_text() == 'Jan 15 2020'
1000+
assert ax.get_xticklabels()[1].get_text() == 'Feb 01 2020'
1001+
1002+
1003+
def test_epoch2num():
1004+
mdates._reset_epoch_test_example()
1005+
mdates.set_epoch('0000-12-31')
1006+
assert mdates.epoch2num(86400) == 719164.0
1007+
assert mdates.num2epoch(719165.0) == 86400 * 2
1008+
# set back to the default
1009+
mdates._reset_epoch_test_example()
1010+
mdates.set_epoch('1970-01-01T00:00:00')
1011+
assert mdates.epoch2num(86400) == 1.0
1012+
assert mdates.num2epoch(2.0) == 86400 * 2

0 commit comments

Comments
 (0)