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

Skip to content

Commit dfc3a79

Browse files
authored
Merge pull request #10084 from jklymak/doc-better-data-error
DOC: Better error when float on datetime axis
2 parents c998849 + a4759e8 commit dfc3a79

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/matplotlib/axis.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import six
88

9+
import logging
10+
911
from matplotlib import rcParams
1012
import matplotlib.artist as artist
1113
from matplotlib.artist import allow_rasterization
@@ -22,6 +24,8 @@
2224
import numpy as np
2325
import warnings
2426

27+
_log = logging.getLogger(__name__)
28+
2529
GRIDLINE_INTERPOLATION_STEPS = 180
2630

2731

lib/matplotlib/dates.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120

121121
import six
122122
from six.moves import zip
123-
from matplotlib import rcParams
124123
import re
125124
import time
126125
import math
@@ -129,20 +128,22 @@
129128

130129
import warnings
131130

132-
133131
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
134132
MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY,
135133
SECONDLY)
136134
from dateutil.relativedelta import relativedelta
137135
import dateutil.parser
136+
import logging
138137
import numpy as np
139138

140139

141140
import matplotlib
141+
from matplotlib import rcParams
142142
import matplotlib.units as units
143143
import matplotlib.cbook as cbook
144144
import matplotlib.ticker as ticker
145145

146+
_log = logging.getLogger(__name__)
146147

147148
__all__ = ('date2num', 'num2date', 'num2timedelta', 'drange', 'epoch2num',
148149
'num2epoch', 'mx2num', 'DateFormatter',
@@ -282,6 +283,11 @@ def _from_ordinalf(x, tz=None):
282283
tz = _get_rc_timezone()
283284

284285
ix = int(x)
286+
if ix < 1:
287+
raise ValueError('cannot convert {} to a date. This '
288+
'often happens if non-datetime values are passed to '
289+
'an axis that expects datetime objects. '
290+
.format(ix))
285291
dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
286292

287293
remainder = float(x) - ix
@@ -941,7 +947,12 @@ def datalim_to_dt(self):
941947
dmin, dmax = self.axis.get_data_interval()
942948
if dmin > dmax:
943949
dmin, dmax = dmax, dmin
944-
950+
if dmin < 1:
951+
raise ValueError('datalim minimum {} is less than 1 and '
952+
'is an invalid Matplotlib date value. This often '
953+
'happens if you pass a non-datetime '
954+
'value to an axis that has datetime units'
955+
.format(dmin))
945956
return num2date(dmin, self.tz), num2date(dmax, self.tz)
946957

947958
def viewlim_to_dt(self):
@@ -951,7 +962,12 @@ def viewlim_to_dt(self):
951962
vmin, vmax = self.axis.get_view_interval()
952963
if vmin > vmax:
953964
vmin, vmax = vmax, vmin
954-
965+
if vmin < 1:
966+
raise ValueError('view limit minimum {} is less than 1 and '
967+
'is an invalid Matplotlib date value. This '
968+
'often happens if you pass a non-datetime '
969+
'value to an axis that has datetime units'
970+
.format(vmin))
955971
return num2date(vmin, self.tz), num2date(vmax, self.tz)
956972

957973
def _get_unit(self):

0 commit comments

Comments
 (0)