From 6b6b08e4fe13f1de310cd25a184625aed114a178 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 26 Jul 2016 13:02:40 +0200 Subject: [PATCH] BUG: handle outofbounds datetimes in DatetimeConverter --- pandas/tests/plotting/test_datetimelike.py | 8 ++++++++ pandas/tseries/converter.py | 2 +- pandas/tseries/tests/test_converter.py | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 3f09317915254..492b9edff0122 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -1222,6 +1222,14 @@ def test_secondary_y_irregular_ts_xlim(self): self.assertEqual(left, ts_irregular.index.min().toordinal()) self.assertEqual(right, ts_irregular.index.max().toordinal()) + def test_plot_outofbounds_datetime(self): + # 2579 - checking this does not raise + values = [date(1677, 1, 1), date(1677, 1, 2)] + self.plt.plot(values) + + values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)] + self.plt.plot(values) + def _check_plot_works(f, freq=None, series=None, *args, **kwargs): import matplotlib.pyplot as plt diff --git a/pandas/tseries/converter.py b/pandas/tseries/converter.py index fc23f4f99449b..a23e8af3e610c 100644 --- a/pandas/tseries/converter.py +++ b/pandas/tseries/converter.py @@ -216,7 +216,7 @@ def try_parse(values): else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: - pass + values = _dt_to_float_ordinal(values) return values diff --git a/pandas/tseries/tests/test_converter.py b/pandas/tseries/tests/test_converter.py index ceb8660efb9cd..37d9c35639c32 100644 --- a/pandas/tseries/tests/test_converter.py +++ b/pandas/tseries/tests/test_converter.py @@ -77,6 +77,24 @@ def test_conversion_float(self): rs = self.dtc.convert(datetime(2012, 1, 1, 1, 2, 3), None, None) tm.assert_almost_equal(rs, xp, decimals) + def test_conversion_outofbounds_datetime(self): + # 2579 + values = [date(1677, 1, 1), date(1677, 1, 2)] + rs = self.dtc.convert(values, None, None) + xp = converter.dates.date2num(values) + tm.assert_numpy_array_equal(rs, xp) + rs = self.dtc.convert(values[0], None, None) + xp = converter.dates.date2num(values[0]) + self.assertEqual(rs, xp) + + values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)] + rs = self.dtc.convert(values, None, None) + xp = converter.dates.date2num(values) + tm.assert_numpy_array_equal(rs, xp) + rs = self.dtc.convert(values[0], None, None) + xp = converter.dates.date2num(values[0]) + self.assertEqual(rs, xp) + def test_time_formatter(self): self.tc(90000)