8000 dates: remove musec_prec parameter again · matplotlib/matplotlib@06b65a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06b65a8

Browse files
committed
dates: remove musec_prec parameter again
addition of a new kwarg to num2date and _from_ordinalf is too intrusive. Since plotting microsecond time ranges with current dates isn't really possible, we don't need to try, so we can simply always do the musec interval rounding. Insert a hard-coded threshold making microsecond plotting still work for users trying to use the 'early-year' workaround hack.
1 parent d67333e commit 06b65a8

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

lib/matplotlib/dates.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _dt64_to_ordinalf(d):
268268
return dt
269269

270270

271-
def _from_ordinalf(x, tz=None, musec_prec=20):
271+
def _from_ordinalf(x, tz=None):
272272
"""
273273
Convert Gregorian float of the date, preserving hours, minutes,
274274
seconds and microseconds. Return value is a :class:`datetime`.
@@ -277,10 +277,6 @@ def _from_ordinalf(x, tz=None, musec_prec=20):
277277
be the specified :class:`datetime` object corresponding to that time in
278278
timezone `tz`, or if `tz` is `None`, in the timezone specified in
279279
`rcParams['timezone']`.
280-
281-
Since the input date `x` float is unable to preserve microsecond
282-
precision of time representation in non-antique years, the resulting
283-
datetime is rounded to the nearest multiple of `musec_prec`.
284280
"""
285281
if tz is None:
286282
tz = _get_rc_timezone()
@@ -295,7 +291,13 @@ def _from_ordinalf(x, tz=None, musec_prec=20):
295291
microseconds=int(round(remainder * MUSECONDS_PER_DAY)))
296292

297293
# Compensate for rounding errors
298-
if musec_prec > 1:
294+
if x > 30 * 365:
295+
# Since the input date `x` float is unable to preserve
296+
# microsecond precision of time representation in non-antique
297+
# years, the resulting datetime is rounded to the nearest
298+
# multiple of `musec_prec`. A value of 20 is appropriate for
299+
# current dates.
300+
musec_prec = 20
299301
musec = datetime.timedelta(
300302
microseconds=int(round(
301303
dt.microsecond / float(musec_prec)) * musec_prec))
@@ -451,7 +453,7 @@ def num2julian(n):
451453
return n + JULIAN_OFFSET
452454

453455

454-
def num2date(x, tz=None, musec_prec=20):
456+
def num2date(x, tz=None):
455457
"""
456458
Parameters
457459
----------
@@ -460,8 +462,6 @@ def num2date(x, tz=None, musec_prec=20):
460462
since 0001-01-01 00:00:00 UTC, plus one.
461463
tz : string, optional
462464
Timezone of *x* (defaults to rcparams TZ value).
463-
musec_prec : int, optional
464-
Microsecond precision of return value
465465
466466
Returns
467467
-------
@@ -480,12 +480,12 @@ def num2date(x, tz=None, musec_prec=20):
480480
if tz is None:
481481
tz = _get_rc_timezone()
482482
if not cbook.iterable(x):
483-
return _from_ordinalf(x, tz, musec_prec)
483+
return _from_ordinalf(x, tz)
484484
else:
485485
x = np.asarray(x)
486486
if not x.size:
487487
return x
488-
return _from_ordinalf_np_vectorized(x, tz, musec_prec).tolist()
488+
return _from_ordinalf_np_vectorized(x, tz).tolist()
489489

490490

491491
def _ordinalf_to_timedelta(x):
@@ -561,25 +561,23 @@ class DateFormatter(ticker.Formatter):
561561

562562
illegal_s = re.compile(r"((^|[^%])(%%)*%s)")
563563

564-
def __init__(self, fmt, tz=None, musec_prec=20):
564+
def __init__(self, fmt, tz=None):
565565
"""
566566
*fmt* is a :func:`strftime` format string; *tz* is the
567-
:class:`tzinfo` instance, *musec_prec* is the microsecond
568-
rounding precision.
567+
:class:`tzinfo` instance.
569568
"""
570569
if tz is None:
571570
tz = _get_rc_timezone()
572571
self.fmt = fmt
573572
self.tz = tz
574-
self.musec_prec = musec_prec
575573

576574
def __call__(self, x, pos=0):
577575
if x == 0:
578576
raise ValueError('DateFormatter found a value of x=0, which is '
579577
'an illegal date. This usually occurs because '
580578
'you have not informed the axis that it is '
581579
'plotting dates, e.g., with ax.xaxis_date()')
582-
dt = num2date(x, self.tz, self.musec_prec)
580+
dt = num2date(x, self.tz)
583581
return self.strftime(dt, self.fmt)
584582

585583
def set_tzinfo(self, tz):
@@ -806,8 +804,7 @@ def __call__(self, x, pos=None):
806804
self.defaultfmt)
807805

808806
if isinstance(fmt, six.string_types):
809-
musec_prec = 1 if fmt.endswith('%f') else 20
810-
self._formatter = DateFormatter(fmt, self._tz, musec_prec)
807+
self._formatter = DateFormatter(fmt, self._tz)
811808
result = self._formatter(x, pos)
812809
elif callable(fmt):
813810
result = fmt(x, pos)
@@ -953,10 +950,7 @@ def datalim_to_dt(self):
953950
if dmin > dmax:
954951
dmin, dmax = dmax, dmin
955952

956-
mup = 20
957-
if abs(dmax - dmin) < 1000. / MUSECONDS_PER_DAY:
958-
mup = 1
959-
return num2date(dmin, self.tz, mup), num2date(dmax, self.tz, mup)
953+
return num2date(dmin, self.tz), num2date(dmax, self.tz)
960954

961955
def viewlim_to_dt(self):
962956
"""
@@ -966,11 +960,7 @@ def viewlim_to_dt(self):
966960
if vmin > vmax:
967961
vmin, vmax = vmax, vmin
968962

969-
mup = 20
970-
if abs(vmax - vmin) < 1000. / MUSECONDS_PER_DAY:
971-
mup = 1
972-
973-
return num2date(vmin, self.tz, mup), num2date(vmax, self.tz, mup)
963+
return num2date(vmin, self.tz), num2date(vmax, self.tz)
974964

975965
def _get_unit(self):
976966
"""

0 commit comments

Comments
 (0)
0