Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a24a56

Browse filesBrowse files
committed
ENH: Add a byte parsed converter, bytespdate2num, in dates module (extends strpdate2num)
1 parent 532eda4 commit 0a24a56
Copy full SHA for 0a24a56

File tree

Expand file treeCollapse file tree

2 files changed

+28
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+28
-2
lines changed

‎examples/pylab_examples/load_converter.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/load_converter.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/dates.py
+26Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.