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

Skip to content

Commit f0f2d55

Browse files
committed
Merge pull request #4153 from cimarronm/4126_fix
FIX: bytes2pdatenum
2 parents 403edba + 0a24a56 commit f0f2d55

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

examples/pylab_examples/load_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import print_function
2-
from matplotlib.dates import strpdate2num
2+
from matplotlib.dates import bytespdate2num
33
#from matplotlib.mlab import load
44
import numpy as np
55
from pylab import figure, show
@@ -10,7 +10,7 @@
1010

1111
dates, closes = np.loadtxt(
1212
datafile, delimiter=',',
13-
converters={0: strpdate2num('%d-%b-%y')},
13+
converters={0: bytespdate2num('%d-%b-%y')},
1414
skiprows=1, usecols=(0, 2), unpack=True)
1515

1616
fig = figure()

lib/matplotlib/dates.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,32 @@ def __call__(self, s):
261261
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
262262

263263

264+
class bytespdate2num(strpdate2num):
265+
"""
266+
Use this class to parse date strings to matplotlib datenums when
267+
you know the date format string of the date you are parsing. See
268+
:file:`examples/load_demo.py`.
269+
"""
270+
def __init__(self, fmt, encoding='utf-8'):
271+
"""
272+
Args:
273+
fmt: any valid strptime format is supported
274+
encoding: encoding to use on byte input (default: 'utf-8')
275+
"""
276+
super(bytespdate2num, self).__init__(fmt)
277+
self.encoding = encoding
278+
279+
def __call__(self, b):
280+
"""
281+
Args:
282+
b: byte input to be converted
283+
Returns:
284+
A date2num float
285+
"""
286+
s = b.decode(self.encoding)
287+
return super(bytespdate2num, self).__call__(s)
288+
289+
264290
# a version of dateutil.parser.parse that can operate on nump0y arrays
265291
_dateutil_parser_parse_np_vectorized = np.vectorize(dateutil.parser.parse)
266292

0 commit comments

Comments
 (0)