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

Skip to content

Commit 13a11b4

Browse files
committed
Merge branch 'timedalta_rounding' of https://github.com/faucon/matplotlib into faucon-timedalta_rounding
2 parents 94cf18d + 3dd84af commit 13a11b4

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/matplotlib/dates.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,15 @@ def drange(dstart, dend, delta):
299299
delta.microseconds/MUSECONDS_PER_DAY)
300300
f1 = _to_ordinalf(dstart)
301301
f2 = _to_ordinalf(dend)
302-
return np.arange(f1, f2, step)
303-
304-
302+
303+
num = int(np.ceil((f2-f1)/step)) #calculate the difference between dend and dstart in times of delta
304+
dinterval_end = dstart + num*delta #calculate end of the interval which will be generated
305+
if dinterval_end >= dend: #ensure, that an half open interval will be generated [dstart, dend)
306+
dinterval_end -= delta #if the endpoint is greated than dend, just subtract one delta
307+
num -= 1
308+
309+
f2 = _to_ordinalf(dinterval_end) #new float-endpoint
310+
return np.linspace(f1, f2, num+1)
305311

306312
### date tickers and formatters ###
307313

lib/matplotlib/tests/test_dates.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
44
import matplotlib.pyplot as plt
5-
from nose.tools import assert_raises
5+
from nose.tools import assert_raises, assert_equal
66
import warnings
77

88
@image_comparison(baseline_images=['date_empty'])
@@ -69,7 +69,9 @@ def test_too_many_date_ticks():
6969
# setting equal datetimes triggers and expander call in
7070
# transforms.nonsingular which results in too many ticks in the
7171
# DayLocator. This should trigger a Locator.MAXTICKS RuntimeError
72-
warnings.filterwarnings('ignore','Attempting to set identical left==right results\\nin singular transformations; automatically expanding.\\nleft=\d*\.\d*, right=\d*\.\d*',UserWarning,module='matplotlib.axes')
72+
warnings.filterwarnings('ignore',
73+
'Attempting to set identical left==right results\\nin singular transformations; automatically expanding.\\nleft=\d*\.\d*, right=\d*\.\d*',
74+
UserWarning, module='matplotlib.axes')
7375
t0 = datetime.datetime(2000, 1, 20)
7476
tf = datetime.datetime(2000, 1, 20)
7577
fig = plt.figure()
@@ -134,6 +136,31 @@ def test_DateFormatter():
134136
ax.autoscale_view()
135137
fig.autofmt_xdate()
136138

139+
def test_drange():
140+
'''This test should check if drange works as expected, and if all the rounding errors
141+
are fixed'''
142+
from matplotlib import dates
143+
start = datetime.datetime(2011, 1,1, tzinfo=dates.UTC)
144+
end = datetime.datetime(2011, 1, 2, tzinfo=dates.UTC)
145+
delta = datetime.timedelta(hours=1)
146+
#We expect 24 values in drange(start, end, delta), because drange returns dates from
147+
#an half open interval [start, end)
148+
assert_equal(24, len(dates.drange(start, end, delta)))
149+
150+
#if end is a little bit later, we expect the range to contain one element more
151+
end = end +datetime.timedelta(microseconds=1)
152+
assert_equal(25, len(dates.drange(start, end, delta)))
153+
154+
#reset end
155+
end = datetime.datetime(2011, 1, 2, tzinfo=dates.UTC)
156+
157+
#and tst drange with "complicated" floats:
158+
# 4 hours = 1/6 day, this is an "dangerous" float
159+
delta = datetime.timedelta(hours=4)
160+
daterange = dates.drange(start, end, delta)
161+
assert_equal(6, len(daterange))
162+
assert_equal(dates.num2date(daterange[-1]), end-delta)
163+
137164
#@image_comparison(baseline_images=['empty_date_bug'])
138165
@cleanup
139166
@knownfailureif(True)

0 commit comments

Comments
 (0)