Skip to content

Commit 9fdac02

Browse files
VIS: let PeriodConverter handle datetime64 data (#18468)
1 parent 45aa0ce commit 9fdac02

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

pandas/plotting/_converter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _convert_1d(values, units, axis):
244244
if not hasattr(axis, 'freq'):
245245
raise TypeError('Axis must have `freq` set to convert to Periods')
246246
valid_types = (compat.string_types, datetime,
247-
Period, pydt.date, pydt.time)
247+
Period, pydt.date, pydt.time, np.datetime64)
248248
if (isinstance(values, valid_types) or is_integer(values) or
249249
is_float(values)):
250250
return get_datevalue(values, axis.freq)
@@ -263,7 +263,7 @@ def get_datevalue(date, freq):
263263
if isinstance(date, Period):
264264
return date.asfreq(freq).ordinal
265265
elif isinstance(date, (compat.string_types, datetime,
266-
pydt.date, pydt.time)):
266+
pydt.date, pydt.time, np.datetime64)):
267267
return Period(date, freq).ordinal
268268
elif (is_integer(date) or is_float(date) or
269269
(isinstance(date, (np.ndarray, Index)) and (date.size == 1))):

pandas/tests/plotting/test_converter.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,19 @@ def test_conversion(self):
327327
rs = self.pc.convert(Timestamp('2012-1-1'), None, self.axis)
328328
assert rs == xp
329329

330-
# FIXME
331-
# rs = self.pc.convert(
332-
# np_datetime64_compat('2012-01-01'), None, self.axis)
333-
# assert rs == xp
334-
#
335-
# rs = self.pc.convert(
336-
# np_datetime64_compat('2012-01-01 00:00:00+0000'),
337-
# None, self.axis)
338-
# assert rs == xp
339-
#
340-
# rs = self.pc.convert(np.array([
341-
# np_datetime64_compat('2012-01-01 00:00:00+0000'),
342-
# np_datetime64_compat('2012-01-02 00:00:00+0000')]),
343-
# None, self.axis)
344-
# assert rs[0] == xp
330+
rs = self.pc.convert(
331+
np_datetime64_compat('2012-01-01'), None, self.axis)
332+
assert rs == xp
333+
334+
rs = self.pc.convert(
335+
np_datetime64_compat('2012-01-01 00:00:00+0000'), None, self.axis)
336+
assert rs == xp
337+
338+
rs = self.pc.convert(np.array([
339+
np_datetime64_compat('2012-01-01 00:00:00+0000'),
340+
np_datetime64_compat('2012-01-02 00:00:00+0000')]),
341+
None, self.axis)
342+
assert rs[0] == xp
345343

346344
def test_integer_passthrough(self):
347345
# GH9012

pandas/tests/plotting/test_datetimelike.py

+13
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,19 @@ def test_overlapping_datetime(self):
14491449
s2.plot(ax=ax)
14501450
s1.plot(ax=ax)
14511451

1452+
@pytest.mark.xfail(reason="GH9053 matplotlib does not use"
1453+
" ax.xaxis.converter")
1454+
def test_add_matplotlib_datetime64(self):
1455+
# GH9053 - ensure that a plot with PeriodConverter still understands
1456+
# datetime64 data. This still fails because matplotlib overrides the
1457+
# ax.xaxis.converter with a DatetimeConverter
1458+
s = Series(np.random.randn(10),
1459+
index=date_range('1970-01-02', periods=10))
1460+
ax = s.plot()
1461+
ax.plot(s.index, s.values, color='g')
1462+
l1, l2 = ax.lines
1463+
tm.assert_numpy_array_equal(l1.get_xydata(), l2.get_xydata())
1464+
14521465

14531466
def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
14541467
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)