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

Skip to content

Commit a6a96e3

Browse files
committed
FIX: make error locator better
1 parent 2c29f66 commit a6a96e3

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/matplotlib/dates.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,11 +1414,9 @@ def tick_values(self, vmin, vmax):
14141414
vmin = vmin.replace(year=ymin, **self.replaced)
14151415
try:
14161416
ticks = [vmin.astimezone(self.tz)]
1417-
except ValueError:
1417+
except ValueError as e:
14181418
raise ValueError('naive datetime objects cannot be used '
1419-
'with matplotlib for python < 3.6; '
1420-
'astimezone() cannot '
1421-
'be applied to a naive datetime.')
1419+
'with matplotlib for python < 3.6; ') from e
14221420
while True:
14231421
dt = ticks[-1]
14241422
if dt.year >= ymax:

lib/matplotlib/tests/test_dates.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,7 @@ def _create_auto_date_locator(date1, date2):
507507
assert list(map(str, mdates.num2date(locator()))) == expected
508508

509509

510-
@pytest.mark.pytz
511-
@pytest.mark.skipif(not __has_pytz(), reason="Requires pytz")
512510
def test_auto_date_locator_intmult_tz():
513-
import pytz
514-
515511
def _create_auto_date_locator(date1, date2, tz):
516512
locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz)
517513
locator.create_dummy_axis()
@@ -572,8 +568,8 @@ def _create_auto_date_locator(date1, date2, tz):
572568
]
573569
)
574570

575-
tz = pytz.timezone('US/Pacific')
576-
d1 = tz.localize(datetime.datetime(1997, 1, 1))
571+
tz = dateutil.tz.gettz('Canada/Pacific')
572+
d1 = datetime.datetime(1997, 1, 1, tzinfo=tz)
577573
for t_delta, expected in results:
578574
with rc_context({'_internal.classic_mode': False}):
579575
d2 = d1 + t_delta
@@ -726,6 +722,29 @@ def attach_tz(dt, zi):
726722
_test_rrulewrapper(attach_tz, pytz.timezone)
727723

728724

725+
@pytest.mark.pytz
726+
@pytest.mark.skipif(not __has_pytz(), reason="Requires pytz")
727+
def test_yearlocator_pytz():
728+
import pytz
729+
730+
tz = pytz.timezone('America/New_York')
731+
x = [datetime.datetime(2010, 1, 1).astimezone(tz)
732+
+ datetime.timedelta(i) for i in range(2000)]
733+
locator = mdates.AutoDateLocator(interval_multiples=False, tz=tz)
734+
locator.create_dummy_axis()
735+
locator.set_view_interval(mdates.date2num(x[0]),
736+
mdates.date2num(x[-1]))
737+
738+
np.testing.assert_allclose([734138.20833333, 734503.20833333,
739+
734869.20833333, 735234.20833333,
740+
735599.20833333], locator())
741+
expected = ['2011-01-01 00:00:00-05:00', '2012-01-01 00:00:00-05:00',
742+
'2013-01-01 00:00:00-05:00', '2014-01-01 00:00:00-05:00',
743+
'2015-01-01 00:00:00-05:00']
744+
st = list(map(str, mdates.num2date(locator(), tz=tz)))
745+
assert st == expected
746+
747+
729748
def test_DayLocator():
730749
with pytest.raises(ValueError):
731750
mdates.DayLocator(interval=-1)

0 commit comments

Comments
 (0)