Skip to content

Commit ad5fe9c

Browse files
mroeschketorext
andauthored
Backport PR #55138: BUG: Silence Period[B] warnings in plotting code (#55378)
* Backport PR #55138: BUG: Silence `Period[B]` warnings in plotting code * Don't build in parallel to see error * Remove period call * Another suppress? --------- Co-authored-by: torext <[email protected]>
1 parent 0191caf commit ad5fe9c

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

doc/source/whatsnew/v2.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Bug fixes
2424
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`)
2525
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
2626
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
27-
-
27+
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
2828

2929
.. ---------------------------------------------------------------------------
3030
.. _whatsnew_212.other:

pandas/plotting/_matplotlib/converter.py

+52-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Final,
1515
cast,
1616
)
17+
import warnings
1718

1819
import matplotlib.dates as mdates
1920
from matplotlib.ticker import (
@@ -243,18 +244,29 @@ def _convert_1d(values, units, axis):
243244
if not hasattr(axis, "freq"):
244245
raise TypeError("Axis must have `freq` set to convert to Periods")
245246
valid_types = (str, datetime, Period, pydt.date, pydt.time, np.datetime64)
246-
if isinstance(values, valid_types) or is_integer(values) or is_float(values):
247-
return get_datevalue(values, axis.freq)
248-
elif isinstance(values, PeriodIndex):
249-
return values.asfreq(axis.freq).asi8
250-
elif isinstance(values, Index):
251-
return values.map(lambda x: get_datevalue(x, axis.freq))
252-
elif lib.infer_dtype(values, skipna=False) == "period":
253-
# https://github.com/pandas-dev/pandas/issues/24304
254-
# convert ndarray[period] -> PeriodIndex
255-
return PeriodIndex(values, freq=axis.freq).asi8
256-
elif isinstance(values, (list, tuple, np.ndarray, Index)):
257-
return [get_datevalue(x, axis.freq) for x in values]
247+
with warnings.catch_warnings():
248+
warnings.filterwarnings(
249+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
250+
)
251+
warnings.filterwarnings(
252+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
253+
)
254+
if (
255+
isinstance(values, valid_types)
256+
or is_integer(values)
257+
or is_float(values)
258+
):
259+
return get_datevalue(values, axis.freq)
260+
elif isinstance(values, PeriodIndex):
261+
return values.asfreq(axis.freq).asi8
262+
elif isinstance(values, Index):
263+
return values.map(lambda x: get_datevalue(x, axis.freq))
264+
elif lib.infer_dtype(values, skipna=False) == "period":
265+
# https://github.com/pandas-dev/pandas/issues/24304
266+
# convert ndarray[period] -> PeriodIndex
267+
return PeriodIndex(values, freq=axis.freq).asi8
268+
elif isinstance(values, (list, tuple, np.ndarray, Index)):
269+
return [get_datevalue(x, axis.freq) for x in values]
258270
return values
259271

260272

@@ -567,14 +579,30 @@ def _daily_finder(vmin, vmax, freq: BaseOffset):
567579
# save this for later usage
568580
vmin_orig = vmin
569581

570-
(vmin, vmax) = (
571-
Period(ordinal=int(vmin), freq=freq),
572-
Period(ordinal=int(vmax), freq=freq),
573-
)
582+
with warnings.catch_warnings():
583+
warnings.filterwarnings(
584+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
585+
)
586+
warnings.filterwarnings(
587+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
588+
)
589+
(vmin, vmax) = (
590+
Period(ordinal=int(vmin), freq=freq),
591+
Period(ordinal=int(vmax), freq=freq),
592+
)
574593
assert isinstance(vmin, Period)
575594
assert isinstance(vmax, Period)
576595
span = vmax.ordinal - vmin.ordinal + 1
577-
dates_ = period_range(start=vmin, end=vmax, freq=freq)
596+
597+
with warnings.catch_warnings():
598+
warnings.filterwarnings(
599+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
600+
)
601+
warnings.filterwarnings(
602+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
603+
)
604+
dates_ = period_range(start=vmin, end=vmax, freq=freq)
605+
578606
# Initialize the output
579607
info = np.zeros(
580608
span, dtype=[("val", np.int64), ("maj", bool), ("min", bool), ("fmt", "|S20")]
@@ -1072,7 +1100,13 @@ def __call__(self, x, pos: int = 0) -> str:
10721100
fmt = self.formatdict.pop(x, "")
10731101
if isinstance(fmt, np.bytes_):
10741102
fmt = fmt.decode("utf-8")
1075-
period = Period(ordinal=int(x), freq=self.freq)
1103+
with warnings.catch_warnings():
1104+
warnings.filterwarnings(
1105+
"ignore",
1106+
"Period with BDay freq is deprecated",
1107+
category=FutureWarning,
1108+
)
1109+
period = Period(ordinal=int(x), freq=self.freq)
10761110
assert isinstance(period, Period)
10771111
return period.strftime(fmt)
10781112

pandas/tests/plotting/frame/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,15 @@ def test_secondary_y(self, secondary_y):
24872487
assert ax.get_ylim() == (0, 100)
24882488
assert ax.get_yticks()[0] == 99
24892489

2490+
@pytest.mark.slow
2491+
def test_plot_no_warning(self):
2492+
# GH 55138
2493+
# TODO(3.0): this can be removed once Period[B] deprecation is enforced
2494+
df = tm.makeTimeDataFrame()
2495+
with tm.assert_produces_warning(False):
2496+
_ = df.plot()
2497+
_ = df.T.plot()
2498+
24902499

24912500
def _generate_4_axes_via_gridspec():
24922501
import matplotlib.pyplot as plt

pandas/tests/plotting/test_series.py

+7
Original file line numberDiff line numberDiff line change
@@ -973,3 +973,10 @@ def test_series_none_color(self):
973973
ax = series.plot(color=None)
974974
expected = _unpack_cycler(mpl.pyplot.rcParams)[:1]
975975
_check_colors(ax.get_lines(), linecolors=expected)
976+
977+
@pytest.mark.slow
978+
def test_plot_no_warning(self, ts):
979+
# GH 55138
980+
# TODO(3.0): this can be removed once Period[B] deprecation is enforced
981+
with tm.assert_produces_warning(False):
982+
_ = ts.plot()

0 commit comments

Comments
 (0)