From 9acc4bf199ce25e0afd9a7ceae8070e46ea4fd4f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 24 Nov 2017 16:13:17 +0100 Subject: [PATCH 1/3] VIS: let PeriodConverter handle datetime64 data --- pandas/plotting/_converter.py | 4 +-- pandas/tests/plotting/test_converter.py | 29 +++++++++++----------- pandas/tests/plotting/test_datetimelike.py | 11 ++++++++ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pandas/plotting/_converter.py b/pandas/plotting/_converter.py index 0f06d87726905..90e355f66cb3c 100644 --- a/pandas/plotting/_converter.py +++ b/pandas/plotting/_converter.py @@ -141,7 +141,7 @@ def _convert_1d(values, units, axis): if not hasattr(axis, 'freq'): raise TypeError('Axis must have `freq` set to convert to Periods') valid_types = (compat.string_types, datetime, - Period, pydt.date, pydt.time) + Period, pydt.date, pydt.time, np.datetime64) if (isinstance(values, valid_types) or is_integer(values) or is_float(values)): return get_datevalue(values, axis.freq) @@ -160,7 +160,7 @@ def get_datevalue(date, freq): if isinstance(date, Period): return date.asfreq(freq).ordinal elif isinstance(date, (compat.string_types, datetime, - pydt.date, pydt.time)): + pydt.date, pydt.time, np.datetime64)): return Period(date, freq).ordinal elif (is_integer(date) or is_float(date) or (isinstance(date, (np.ndarray, Index)) and (date.size == 1))): diff --git a/pandas/tests/plotting/test_converter.py b/pandas/tests/plotting/test_converter.py index e1f64bed5598d..ad775cb97ee59 100644 --- a/pandas/tests/plotting/test_converter.py +++ b/pandas/tests/plotting/test_converter.py @@ -182,21 +182,20 @@ def test_conversion(self): rs = self.pc.convert(Timestamp('2012-1-1'), None, self.axis) assert rs == xp - # FIXME - # rs = self.pc.convert( - # np_datetime64_compat('2012-01-01'), None, self.axis) - # assert rs == xp - # - # rs = self.pc.convert( - # np_datetime64_compat('2012-01-01 00:00:00+0000'), - # None, self.axis) - # assert rs == xp - # - # rs = self.pc.convert(np.array([ - # np_datetime64_compat('2012-01-01 00:00:00+0000'), - # np_datetime64_compat('2012-01-02 00:00:00+0000')]), - # None, self.axis) - # assert rs[0] == xp + rs = self.pc.convert( + np_datetime64_compat('2012-01-01'), None, self.axis) + assert rs == xp + + rs = self.pc.convert( + np_datetime64_compat('2012-01-01 00:00:00+0000'), + None, self.axis) + assert rs == xp + + rs = self.pc.convert(np.array([ + np_datetime64_compat('2012-01-01 00:00:00+0000'), + np_datetime64_compat('2012-01-02 00:00:00+0000')]), + None, self.axis) + assert rs[0] == xp def test_integer_passthrough(self): # GH9012 diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index d66012e2a56a0..b5afac1c85750 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -1434,6 +1434,17 @@ def test_overlapping_datetime(self): s2.plot(ax=ax) s1.plot(ax=ax) + @pytest.mark.xfail + def test_add_matplotlib_datetime64(self): + # GH9053 ensure that a plot with PeriodConverter still understands + # datetime64 data + s = Series(np.random.randn(10), + index=date_range('1970-01-02', periods=10)) + ax = s.plot() + ax.plot(s.index, s.values, color='g') + l1, l2 = ax.lines + tm.assert_numpy_array_equal(l1.get_xydata(), l2.get_xydata()) + def _check_plot_works(f, freq=None, series=None, *args, **kwargs): import matplotlib.pyplot as plt From 86651c377a2fed35ea172c33b8c7872bfca3251f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 21 Jan 2018 21:29:11 +0100 Subject: [PATCH 2/3] pep8 --- pandas/tests/plotting/test_converter.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_converter.py b/pandas/tests/plotting/test_converter.py index 44efd1402b362..47cded19f5300 100644 --- a/pandas/tests/plotting/test_converter.py +++ b/pandas/tests/plotting/test_converter.py @@ -328,12 +328,11 @@ def test_conversion(self): assert rs == xp rs = self.pc.convert( - np_datetime64_compat('2012-01-01'), None, self.axis) + np_datetime64_compat('2012-01-01'), None, self.axis) assert rs == xp rs = self.pc.convert( - np_datetime64_compat('2012-01-01 00:00:00+0000'), - None, self.axis) + np_datetime64_compat('2012-01-01 00:00:00+0000'), None, self.axis) assert rs == xp rs = self.pc.convert(np.array([ From f20f999318627f6e9c9b76a518ef66bf4a6bd855 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 22 Jan 2018 12:00:19 +0100 Subject: [PATCH 3/3] add xfail reason --- pandas/tests/plotting/test_datetimelike.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 25a302842d01e..94adf349fe2cd 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -1449,10 +1449,12 @@ def test_overlapping_datetime(self): s2.plot(ax=ax) s1.plot(ax=ax) - @pytest.mark.xfail + @pytest.mark.xfail(reason="GH9053 matplotlib does not use" + " ax.xaxis.converter") def test_add_matplotlib_datetime64(self): - # GH9053 ensure that a plot with PeriodConverter still understands - # datetime64 data + # GH9053 - ensure that a plot with PeriodConverter still understands + # datetime64 data. This still fails because matplotlib overrides the + # ax.xaxis.converter with a DatetimeConverter s = Series(np.random.randn(10), index=date_range('1970-01-02', periods=10)) ax = s.plot()