Skip to content

VIS: let PeriodConverter handle datetime64 data #18468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/plotting/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,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)
Expand All @@ -263,7 +263,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))):
Expand Down
28 changes: 13 additions & 15 deletions pandas/tests/plotting/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,21 +327,19 @@ 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
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,17 @@ def test_overlapping_datetime(self):
s2.plot(ax=ax)
s1.plot(ax=ax)

@pytest.mark.xfail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a reason to the xfail (ref an issue)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue is already linked, see comment on first line of test (or do you mean in the xfail message itself?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you normally provide a reason= in the xfail itself, which then prints out when the tests run

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
Expand Down