Skip to content

Commit

Permalink
Merge pull request #13319 from anntzer/pdate2num
Browse files Browse the repository at this point in the history
Deprecate dates.{str,bytes}pdate2num.
  • Loading branch information
jklymak committed Jan 30, 2019
2 parents e714048 + ef3e89e commit e7fb714
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/2018-01-31-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Deprecations
````````````

``dates.strpdate2num`` and ``dates.bytespdate2num`` are brittle in the
presence of locale changes, and are deprecated. Use standard datetime
parsers such as `time.strptime` or `dateutil.parser.parse`, and additionally
call `matplotlib.dates.date2num` if you insist on converting to Matplotlib's
internal datetime representation; or use ``dates.datestr2num``.
8 changes: 5 additions & 3 deletions examples/misc/load_converter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
==============
Load Converter
Load converter
==============
This example demonstrates passing a custom converter to `numpy.genfromtxt` to
extract dates from a CSV file.
"""

import dateutil.parser
Expand All @@ -16,9 +18,9 @@

data = np.genfromtxt(
datafile, delimiter=',', names=True,
converters={0: lambda s: dates.date2num(dateutil.parser.parse(s))})
dtype=None, converters={0: dateutil.parser.parse})

fig, ax = plt.subplots()
ax.plot_date(data['Date'], data['High'], '-')
ax.plot(data['Date'], data['High'], '-')
fig.autofmt_xdate()
plt.show()
4 changes: 4 additions & 0 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ def _from_ordinalf(x, tz=None):
_from_ordinalf_np_vectorized = np.vectorize(_from_ordinalf)


@cbook.deprecated(
"3.1", alternative="time.strptime or dateutil.parser.parse or datestr2num")
class strpdate2num(object):
"""
Use this class to parse date strings to matplotlib datenums when
Expand All @@ -333,6 +335,8 @@ def __call__(self, s):
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))


@cbook.deprecated(
"3.1", alternative="time.strptime or dateutil.parser.parse or datestr2num")
class bytespdate2num(strpdate2num):
"""
Use this class to parse date strings to matplotlib datenums when
Expand Down

0 comments on commit e7fb714

Please sign in to comment.