Skip to content

Commit

Permalink
BUG: disallow how keyword in shared-ax plotting (#55953)
Browse files Browse the repository at this point in the history
* BUG: disallow how keyword in shared-ax plotting

* Update doc/source/whatsnew/v2.2.0.rst

Co-authored-by: Matthew Roeschke <[email protected]>

---------

Co-authored-by: Matthew Roeschke <[email protected]>
  • Loading branch information
jbrockmendel and mroeschke authored Nov 14, 2023
1 parent 6f04606 commit d7da0e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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 @@ -427,6 +427,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

0 comments on commit d7da0e6

Please sign in to comment.