Skip to content

Commit 4168df2

Browse files
torextmroeschke
authored andcommitted
Backport PR pandas-dev#55138: BUG: Silence Period[B] warnings in plotting code
1 parent 78a5500 commit 4168df2

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

doc/source/whatsnew/v2.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Fixed regressions
2121

2222
Bug fixes
2323
~~~~~~~~~
24-
-
24+
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
2525
-
2626

2727
.. ---------------------------------------------------------------------------

pandas/plotting/_matplotlib/converter.py

+45-14
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

@@ -574,7 +586,20 @@ def _daily_finder(vmin, vmax, freq: BaseOffset):
574586
assert isinstance(vmin, Period)
575587
assert isinstance(vmax, Period)
576588
span = vmax.ordinal - vmin.ordinal + 1
577-
dates_ = period_range(start=vmin, end=vmax, freq=freq)
589+
590+
with warnings.catch_warnings():
591+
warnings.filterwarnings(
592+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
593+
)
594+
warnings.filterwarnings(
595+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
596+
)
597+
dates_ = period_range(
598+
start=Period(ordinal=vmin, freq=freq),
599+
end=Period(ordinal=vmax, freq=freq),
600+
freq=freq,
601+
)
602+
578603
# Initialize the output
579604
info = np.zeros(
580605
span, dtype=[("val", np.int64), ("maj", bool), ("min", bool), ("fmt", "|S20")]
@@ -1072,7 +1097,13 @@ def __call__(self, x, pos: int = 0) -> str:
10721097
fmt = self.formatdict.pop(x, "")
10731098
if isinstance(fmt, np.bytes_):
10741099
fmt = fmt.decode("utf-8")
1075-
period = Period(ordinal=int(x), freq=self.freq)
1100+
with warnings.catch_warnings():
1101+
warnings.filterwarnings(
1102+
"ignore",
1103+
"Period with BDay freq is deprecated",
1104+
category=FutureWarning,
1105+
)
1106+
period = Period(ordinal=int(x), freq=self.freq)
10761107
assert isinstance(period, Period)
10771108
return period.strftime(fmt)
10781109

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)