Skip to content

Commit 35b93d2

Browse files
author
Paul G
committed
Support numpy arrays in DayLocator, WeekdayLocator and MonthLocator when dateutil <= 2.3
Signed-off-by: Paul G <pg@example.com>
1 parent 46a4a0a commit 35b93d2

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

lib/matplotlib/dates.py

+34-14
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,8 @@ def __init__(self, tz=None, minticks=5, maxticks=None,
927927
MICROSECONDLY: [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000,
928928
5000, 10000, 20000, 50000, 100000, 200000, 500000,
929929
1000000]}
930-
self._byranges = [None, list(xrange(1, 13)), list(xrange(1, 32)),
931-
list(xrange(0, 24)), list(xrange(0, 60)),
932-
list(xrange(0, 60)), None]
930+
self._byranges = [None, range(1, 13), range(1, 32),
931+
range(0, 24), range(0, 60), range(0, 60), None]
933932

934933
def __call__(self):
935934
'Return the locations of the ticks'
@@ -1129,10 +1128,16 @@ def __init__(self, bymonth=None, bymonthday=1, interval=1, tz=None):
11291128
example, if ``interval=2``, mark every second occurance.
11301129
"""
11311130
if bymonth is None:
1132-
bymonth = list(xrange(1, 13))
1133-
o = rrulewrapper(MONTHLY, bymonth=bymonth, bymonthday=bymonthday,
1131+
bymonth = range(1, 13)
1132+
elif isinstance(bymonth, np.ndarray):
1133+
# This fixes a bug in dateutil <= 2.3 which prevents the use of
1134+
# numpy arrays in (among other things) the bymonthday, byweekday
1135+
# and bymonth parameters.
1136+
bymonth = [x.item() for x in bymonth.astype(int)]
1137+
1138+
rule = rrulewrapper(MONTHLY, bymonth=bymonth, bymonthday=bymonthday,
11341139
interval=interval, **self.hms0d)
1135-
RRuleLocator.__init__(self, o, tz)
1140+
RRuleLocator.__init__(self, rule, tz)
11361141

11371142

11381143
class WeekdayLocator(RRuleLocator):
@@ -1152,9 +1157,15 @@ def __init__(self, byweekday=1, interval=1, tz=None):
11521157
*interval* specifies the number of weeks to skip. For example,
11531158
``interval=2`` plots every second week.
11541159
"""
1155-
o = rrulewrapper(DAILY, byweekday=byweekday,
1156-
interval=interval, **self.hms0d)
1157-
RRuleLocator.__init__(self, o, tz)
1160+
if isinstance(byweekday, np.ndarray):
1161+
# This fixes a bug in dateutil <= 2.3 which prevents the use of
1162+
# numpy arrays in (among other things) the bymonthday, byweekday
1163+
# and bymonth parameters.
1164+
[x.item() for x in byweekday.astype(int)]
1165+
1166+
rule = rrulewrapper(DAILY, byweekday=byweekday,
1167+
interval=interval, **self.hms0d)
1168+
RRuleLocator.__init__(self, rule, tz)
11581169

11591170

11601171
class DayLocator(RRuleLocator):
@@ -1170,9 +1181,15 @@ def __init__(self, bymonthday=None, interval=1, tz=None):
11701181
Default is to tick every day of the month: ``bymonthday=range(1,32)``
11711182
"""
11721183
if bymonthday is None:
1173-
bymonthday = list(xrange(1, 32))
1184+
bymonthday = range(1, 32)
1185+
elif isinstance(bymonthday, np.ndarray):
1186+
# This fixes a bug in dateutil <= 2.3 which prevents the use of
1187+
# numpy arrays in (among other things) the bymonthday, byweekday
1188+
# and bymonth parameters.
1189+
bymonthday = [x.item() for x in bymonthday.astype(int)]
1190+
11741191
rule = rrulewrapper(DAILY, bymonthday=bymonthday,
1175-
interval=interval, **self.hms0d)
1192+
interval=interval, **self.hms0d)
11761193
RRuleLocator.__init__(self, rule, tz)
11771194

11781195

@@ -1189,7 +1206,8 @@ def __init__(self, byhour=None, interval=1, tz=None):
11891206
example, if ``interval=2``, mark every second occurrence.
11901207
"""
11911208
if byhour is None:
1192-
byhour = list(xrange(24))
1209+
byhour = range(24)
1210+
11931211
rule = rrulewrapper(HOURLY, byhour=byhour, interval=interval,
11941212
byminute=0, bysecond=0)
11951213
RRuleLocator.__init__(self, rule, tz)
@@ -1208,7 +1226,8 @@ def __init__(self, byminute=None, interval=1, tz=None):
12081226
example, if ``interval=2``, mark every second occurrence.
12091227
"""
12101228
if byminute is None:
1211-
byminute = list(xrange(60))
1229+
byminute = range(60)
1230+
12121231
rule = rrulewrapper(MINUTELY, byminute=byminute, interval=interval,
12131232
bysecond=0)
12141233
RRuleLocator.__init__(self, rule, tz)
@@ -1228,7 +1247,8 @@ def __init__(self, bysecond=None, interval=1, tz=None):
12281247
12291248
"""
12301249
if bysecond is None:
1231-
bysecond = list(xrange(60))
1250+
bysecond = range(60)
1251+
12321252
rule = rrulewrapper(SECONDLY, bysecond=bysecond, interval=interval)
12331253
RRuleLocator.__init__(self, rule, tz)
12341254

0 commit comments

Comments
 (0)