File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from __future__ import print_function
2- from matplotlib .dates import strpdate2num
2+ from matplotlib .dates import bytespdate2num
33#from matplotlib.mlab import load
44import numpy as np
55from pylab import figure , show
1010
1111dates , 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
1616fig = figure ()
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments