File tree 2 files changed +28
-2
lines changed 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change 1
1
from __future__ import print_function
2
- from matplotlib .dates import strpdate2num
2
+ from matplotlib .dates import bytespdate2num
3
3
#from matplotlib.mlab import load
4
4
import numpy as np
5
5
from pylab import figure , show
10
10
11
11
dates , closes = np .loadtxt (
12
12
datafile , delimiter = ',' ,
13
- converters = {0 : strpdate2num ('%d-%b-%y' )},
13
+ converters = {0 : bytespdate2num ('%d-%b-%y' )},
14
14
skiprows = 1 , usecols = (0 , 2 ), unpack = True )
15
15
16
16
fig = figure ()
Original file line number Diff line number Diff line change @@ -261,6 +261,32 @@ def __call__(self, s):
261
261
return date2num (datetime .datetime (* time .strptime (s , self .fmt )[:6 ]))
262
262
263
263
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
+
264
290
# a version of dateutil.parser.parse that can operate on nump0y arrays
265
291
_dateutil_parser_parse_np_vectorized = np .vectorize (dateutil .parser .parse )
266
292
You can’t perform that action at this time.
0 commit comments