Skip to content

BUG: disallow how keyword in shared-ax plotting #55953

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 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ Period
Plotting
^^^^^^^^
- Bug in :meth:`DataFrame.plot.box` with ``vert=False`` and a matplotlib ``Axes`` created with ``sharey=True`` (:issue:`54941`)
- Bug in :meth:`Series.plot` when reusing an ``ax`` object failing to raise when a ``how`` keyword is passed (:issue:`55953`)
-

Groupby/resample/rolling
Expand Down
9 changes: 8 additions & 1 deletion pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@

def maybe_resample(series: Series, ax: Axes, kwargs: dict[str, Any]):
# resample against axes freq if necessary

if "how" in kwargs:
raise ValueError(
"'how' is not a valid keyword for plotting functions. If plotting "
"multiple objects on shared axes, resample manually first."
)

freq, ax_freq = _get_freq(ax, series)

if freq is None: # pragma: no cover
Expand All @@ -79,7 +86,7 @@ def maybe_resample(series: Series, ax: Axes, kwargs: dict[str, Any]):
)
freq = ax_freq
elif _is_sup(freq, ax_freq): # one is weekly
how = kwargs.pop("how", "last")
how = "last"
series = getattr(series.resample("D"), how)().dropna()
series = getattr(series.resample(ax_freq), how)().dropna()
freq = ax_freq
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,21 @@ def test_nat_handling(self):
assert s.index.min() <= Series(xdata).min()
assert Series(xdata).max() <= s.index.max()

def test_to_weekly_resampling_disallow_how_kwd(self):
idxh = date_range("1/1/1999", periods=52, freq="W")
idxl = date_range("1/1/1999", periods=12, freq="ME")
high = Series(np.random.default_rng(2).standard_normal(len(idxh)), idxh)
low = Series(np.random.default_rng(2).standard_normal(len(idxl)), idxl)
_, ax = mpl.pyplot.subplots()
high.plot(ax=ax)

msg = (
"'how' is not a valid keyword for plotting functions. If plotting "
"multiple objects on shared axes, resample manually first."
)
with pytest.raises(ValueError, match=msg):
low.plot(ax=ax, how="foo")

def test_to_weekly_resampling(self):
idxh = date_range("1/1/1999", periods=52, freq="W")
idxl = date_range("1/1/1999", periods=12, freq="ME")
Expand Down