Skip to content

Commit a601827

Browse files
jbrockmendelmroeschke
authored andcommitted
BUG: disallow how keyword in shared-ax plotting (#55953)
* 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]>
1 parent 0be2bf5 commit a601827

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ Period
427427
Plotting
428428
^^^^^^^^
429429
- Bug in :meth:`DataFrame.plot.box` with ``vert=False`` and a matplotlib ``Axes`` created with ``sharey=True`` (:issue:`54941`)
430+
- Bug in :meth:`Series.plot` when reusing an ``ax`` object failing to raise when a ``how`` keyword is passed (:issue:`55953`)
430431
-
431432

432433
Groupby/resample/rolling

pandas/plotting/_matplotlib/timeseries.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161

6262
def maybe_resample(series: Series, ax: Axes, kwargs: dict[str, Any]):
6363
# resample against axes freq if necessary
64+
65+
if "how" in kwargs:
66+
raise ValueError(
67+
"'how' is not a valid keyword for plotting functions. If plotting "
68+
"multiple objects on shared axes, resample manually first."
69+
)
70+
6471
freq, ax_freq = _get_freq(ax, series)
6572

6673
if freq is None: # pragma: no cover
@@ -79,7 +86,7 @@ def maybe_resample(series: Series, ax: Axes, kwargs: dict[str, Any]):
7986
)
8087
freq = ax_freq
8188
elif _is_sup(freq, ax_freq): # one is weekly
82-
how = kwargs.pop("how", "last")
89+
how = "last"
8390
series = getattr(series.resample("D"), how)().dropna()
8491
series = getattr(series.resample(ax_freq), how)().dropna()
8592
freq = ax_freq

pandas/tests/plotting/test_datetimelike.py

+15
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,21 @@ def test_nat_handling(self):
916916
assert s.index.min() <= Series(xdata).min()
917917
assert Series(xdata).max() <= s.index.max()
918918

919+
def test_to_weekly_resampling_disallow_how_kwd(self):
920+
idxh = date_range("1/1/1999", periods=52, freq="W")
921+
idxl = date_range("1/1/1999", periods=12, freq="ME")
922+
high = Series(np.random.default_rng(2).standard_normal(len(idxh)), idxh)
923+
low = Series(np.random.default_rng(2).standard_normal(len(idxl)), idxl)
924+
_, ax = mpl.pyplot.subplots()
925+
high.plot(ax=ax)
926+
927+
msg = (
928+
"'how' is not a valid keyword for plotting functions. If plotting "
929+
"multiple objects on shared axes, resample manually first."
930+
)
931+
with pytest.raises(ValueError, match=msg):
932+
low.plot(ax=ax, how="foo")
933+
919934
def test_to_weekly_resampling(self):
920935
idxh = date_range("1/1/1999", periods=52, freq="W")
921936
idxl = date_range("1/1/1999", periods=12, freq="ME")

0 commit comments

Comments
 (0)