From ef3e89e1d00f043714df6c01fc902d7004c8650c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 29 Jan 2019 22:25:48 +0100 Subject: [PATCH] Deprecate dates.{str,bytes}pdate2num. They are brittle against locale changes and there are replacements in the stdlib (time.strpdate, also brittle against locale changes) or elsewhere (dateutil.parser.parse, more robust), which also do not convert to Matplotlib's internal date representation (as that's not needed for plotting datetime data). --- doc/api/next_api_changes/2018-01-31-AL.rst | 8 ++++++++ examples/misc/load_converter.py | 8 +++++--- lib/matplotlib/dates.py | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 doc/api/next_api_changes/2018-01-31-AL.rst diff --git a/doc/api/next_api_changes/2018-01-31-AL.rst b/doc/api/next_api_changes/2018-01-31-AL.rst new file mode 100644 index 000000000000..ace7046efe0c --- /dev/null +++ b/doc/api/next_api_changes/2018-01-31-AL.rst @@ -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``. diff --git a/examples/misc/load_converter.py b/examples/misc/load_converter.py index 2c3e03f73b89..5f0d7940f1cc 100644 --- a/examples/misc/load_converter.py +++ b/examples/misc/load_converter.py @@ -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 @@ -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() diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index ed0f9ae1e45b..016656ab3f61 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -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 @@ -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