From cfd51c105a185d004569d8ab9380396d3317f87b Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 30 May 2020 19:49:47 +0100 Subject: [PATCH 1/6] remove unreachable code, type data and return --- pandas/plotting/_matplotlib/timeseries.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index e73a109449d62..2af3ac1cbc6cf 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -1,7 +1,7 @@ # TODO: Use the fact that axis can have units to simplify the process import functools -from typing import Optional +from typing import TYPE_CHECKING, Optional import numpy as np @@ -24,6 +24,10 @@ from pandas.tseries.frequencies import is_subperiod, is_superperiod from pandas.tseries.offsets import DateOffset +if TYPE_CHECKING: + from pandas._typing import FrameOrSeries # noqa: F401 + + # --------------------------------------------------------------------- # Plotting functions and monkey patches @@ -188,7 +192,8 @@ def _get_freq(ax, series): return freq, ax_freq -def _use_dynamic_x(ax, data): +def _use_dynamic_x(ax, data: "FrameOrSeries") -> bool: + freq = _get_index_freq(data) ax_freq = _get_ax_freq(ax) @@ -203,9 +208,6 @@ def _use_dynamic_x(ax, data): freq = get_period_alias(freq) - if freq is None: - return False - # FIXME: hack this for 0.10.1, creating more technical debt...sigh if isinstance(data.index, ABCDatetimeIndex): base = get_freq_code(freq)[0] From 3d190aab326c2b1c0a4079f78ddf7d5127b327ac Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 31 May 2020 07:58:09 +0100 Subject: [PATCH 2/6] frameorseriesunion --- pandas/plotting/_matplotlib/timeseries.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 2af3ac1cbc6cf..06ec7b3b0d822 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -1,12 +1,13 @@ # TODO: Use the fact that axis can have units to simplify the process import functools -from typing import TYPE_CHECKING, Optional +from typing import Optional import numpy as np from pandas._libs.tslibs.frequencies import FreqGroup, base_and_stride, get_freq_code from pandas._libs.tslibs.period import Period +from pandas._typing import FrameOrSeriesUnion from pandas.core.dtypes.generic import ( ABCDatetimeIndex, @@ -24,10 +25,6 @@ from pandas.tseries.frequencies import is_subperiod, is_superperiod from pandas.tseries.offsets import DateOffset -if TYPE_CHECKING: - from pandas._typing import FrameOrSeries # noqa: F401 - - # --------------------------------------------------------------------- # Plotting functions and monkey patches @@ -192,7 +189,7 @@ def _get_freq(ax, series): return freq, ax_freq -def _use_dynamic_x(ax, data: "FrameOrSeries") -> bool: +def _use_dynamic_x(ax, data: "FrameOrSeriesUnion") -> bool: freq = _get_index_freq(data) ax_freq = _get_ax_freq(ax) From 6ba5b6bd2f0537dd40a98301f0127c538a1c9bbf Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 31 May 2020 08:54:36 +0100 Subject: [PATCH 3/6] add test case which hits uncovered line --- pandas/plotting/_matplotlib/timeseries.py | 3 +++ pandas/tests/plotting/test_datetimelike.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 06ec7b3b0d822..f08deaaa31ea3 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -205,6 +205,9 @@ def _use_dynamic_x(ax, data: "FrameOrSeriesUnion") -> bool: freq = get_period_alias(freq) + if freq is None: + return False + # FIXME: hack this for 0.10.1, creating more technical debt...sigh if isinstance(data.index, ABCDatetimeIndex): base = get_freq_code(freq)[0] diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 7dcb692e29337..5cae61a82166e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -16,7 +16,7 @@ from pandas.core.resample import DatetimeIndex from pandas.tests.plotting.common import TestPlotBase -from pandas.tseries.offsets import DateOffset +from pandas.tseries.offsets import DateOffset, WeekOfMonth @td.skip_if_no_mpl @@ -325,6 +325,17 @@ def test_business_freq_convert(self): idx = ax.get_lines()[0].get_xdata() assert PeriodIndex(data=idx).freqstr == "M" + def test_freq_with_no_period_alias(self): + freq = WeekOfMonth() + bts = tm.makeTimeSeries(5).asfreq(freq) + _, ax = self.plt.subplots() + bts.plot(ax=ax) + assert ax.get_lines()[0].get_xydata()[0, 0] == bts.index[0].toordinal() + idx = ax.get_lines()[0].get_xdata() + msg = "freq not specified and cannot be inferred" + with pytest.raises(ValueError, match=msg): + PeriodIndex(data=idx) + def test_nonzero_base(self): # GH2571 idx = date_range("2012-12-20", periods=24, freq="H") + timedelta(minutes=30) From f0d680038d5ee777f76ab2c076d4e1b21161c025 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 31 May 2020 08:55:27 +0100 Subject: [PATCH 4/6] revert blank line --- pandas/plotting/_matplotlib/timeseries.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index f08deaaa31ea3..e7ecf0139855c 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -190,7 +190,6 @@ def _get_freq(ax, series): def _use_dynamic_x(ax, data: "FrameOrSeriesUnion") -> bool: - freq = _get_index_freq(data) ax_freq = _get_ax_freq(ax) From 228901bd7fbc53b8df833555d5c39f2f7ead7dfe Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 2 Jun 2020 19:44:56 +0100 Subject: [PATCH 5/6] add PR number --- pandas/tests/plotting/test_datetimelike.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 5cae61a82166e..a4515c17820d9 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -326,6 +326,7 @@ def test_business_freq_convert(self): assert PeriodIndex(data=idx).freqstr == "M" def test_freq_with_no_period_alias(self): + # GH34487 freq = WeekOfMonth() bts = tm.makeTimeSeries(5).asfreq(freq) _, ax = self.plt.subplots() From 8c15facff4ca0f682eab7e51c8e2c8543d53aa76 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 2 Jun 2020 19:46:35 +0100 Subject: [PATCH 6/6] Update pandas/tests/plotting/test_datetimelike.py --- pandas/tests/plotting/test_datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index a4515c17820d9..738df5244955a 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -326,7 +326,7 @@ def test_business_freq_convert(self): assert PeriodIndex(data=idx).freqstr == "M" def test_freq_with_no_period_alias(self): - # GH34487 + # GH34487 freq = WeekOfMonth() bts = tm.makeTimeSeries(5).asfreq(freq) _, ax = self.plt.subplots()