Skip to content

BUG: Enable plotting with PeriodIndex with arbitrary frequencies, resolves #14763 #26241

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
merged 16 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 5 additions & 2 deletions pandas/plotting/_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _get_index_freq(data):
def _maybe_convert_index(ax, data):
# tsplot converts automatically, but don't want to convert index
# over and over for DataFrames
if isinstance(data.index, ABCDatetimeIndex):
if isinstance(data.index, (ABCDatetimeIndex, ABCPeriodIndex)):
freq = getattr(data.index, 'freq', None)

if freq is None:
Expand All @@ -279,7 +279,10 @@ def _maybe_convert_index(ax, data):
freq = get_base_alias(freq)
freq = frequencies.get_period_alias(freq)

data = data.to_period(freq=freq)
if isinstance(data.index, ABCDatetimeIndex):
data = data.to_period(freq=freq)
elif isinstance(data.index, ABCPeriodIndex):
data.index = data.index.asfreq(freq=freq)
return data


Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" Test cases for time series specific (freq conversion, etc) """
from datetime import date, datetime, time, timedelta
import itertools
import pickle
import sys

Expand Down Expand Up @@ -33,6 +34,13 @@ def setup_method(self, method):
self.period_df = [DataFrame(np.random.randn(len(x), 3), index=x,
columns=['A', 'B', 'C'])
for x in idx]
mults = [1, 23] # setup periods with multiples of freq.rule_code
freq = [str(mlt) + frq for frq, mlt in itertools.product(freq, mults)]
idx = [period_range('12/31/1999', freq=x, periods=100) for x in freq]
self.period_mlt_ser = [Series(np.random.randn(len(x)), x) for x in idx]
self.period_mlt_df = [DataFrame(np.random.randn(len(x), 3), index=x,
columns=['A', 'B', 'C'])
for x in idx]

freq = ['S', 'T', 'H', 'D', 'W', 'M', 'Q-DEC', 'A', '1B30Min']
idx = [date_range('12/31/1999', freq=x, periods=100) for x in freq]
Expand Down Expand Up @@ -210,6 +218,11 @@ def test_line_plot_period_series(self):
for s in self.period_ser:
_check_plot_works(s.plot, s.index.freq)

@pytest.mark.slow
def test_line_plot_period_mlt_series(self):
for s in self.period_mlt_ser:
_check_plot_works(s.plot, s.index.freq.rule_code)

@pytest.mark.slow
def test_line_plot_datetime_series(self):
for s in self.datetime_ser:
Expand All @@ -220,6 +233,12 @@ def test_line_plot_period_frame(self):
for df in self.period_df:
_check_plot_works(df.plot, df.index.freq)

@pytest.mark.slow
def test_line_plot_period_mlt_frame(self):
for df in self.period_mlt_df:
freq = df.index.asfreq(df.index.freq.rule_code).freq
_check_plot_works(df.plot, freq)

@pytest.mark.slow
def test_line_plot_datetime_frame(self):
for df in self.datetime_df:
Expand Down