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

Skip to content

bytes2pdatenum #4153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/pylab_examples/load_converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function
from matplotlib.dates import strpdate2num
from matplotlib.dates import bytespdate2num
#from matplotlib.mlab import load
import numpy as np
from pylab import figure, show
Expand All @@ -10,7 +10,7 @@

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

fig = figure()
Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ def __call__(self, s):
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))


class bytespdate2num(strpdate2num):
"""
Use this class to parse date strings to matplotlib datenums when
you know the date format string of the date you are parsing. See
:file:`examples/load_demo.py`.
"""
def __init__(self, fmt, encoding='utf-8'):
"""
Args:
fmt: any valid strptime format is supported
encoding: encoding to use on byte input (default: 'utf-8')
"""
super(bytespdate2num, self).__init__(fmt)
self.encoding = encoding

def __call__(self, b):
"""
Args:
b: byte input to be converted
Returns:
A date2num float
"""
s = b.decode(self.encoding)
return super(bytespdate2num, self).__call__(s)


# a version of dateutil.parser.parse that can operate on nump0y arrays
_dateutil_parser_parse_np_vectorized = np.vectorize(dateutil.parser.parse)

Expand Down